-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
95 lines (76 loc) · 1.84 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Example site using Express...
// Include important JS helpers
//require( __dirname +'/helpers.js');
var fs = require('fs'),
http = require('http'),
config = require( __dirname +'/config/app.js'), // get configuration
express = require('express'), // Include express engine
hbs = require('hbs'),
app = express(), // create node server
server = http.createServer(app),
io = require('socket.io').listen(server),
iu = require( __dirname +'/lib/game.js');
var DEV = !(process.env.NODE_ENV == 'production');
// Default APP Configuration
app
.set('views', __dirname + '/views')
.set('view engine', 'html')
.engine("html", require('hbs').__express );
//.use(express.methodOverride());
if( DEV ){
app
.use(express.static(__dirname + '/public'));
} else {
var oneYear = 31557600000;
app
.use(express.static(__dirname + '/public', { maxAge: oneYear }));
}
// Index Route
app.get('/', function(req, res){
res.render('index', {
locals: {
name: 'index',
title: config.name
}
});
});
// About page
app.get('/about', function(req, res){
res.render('about', {
locals: {
name: 'about',
title: config.name
}
});
});
// Scores page
app.get('/scores', function(req, res){
// consider replacing this "hard" call to the file with a memory seek
var file = fs.readFileSync('./data/scores.json');
var scores = JSON.parse(file);
res.render('scores', {
locals: {
name: 'scores',
title: config.name,
scores: scores
}
});
});
// configure handlebars
var blocks = {};
hbs.registerHelper('extend', function(name, context) {
var block = blocks[name];
if (!block) {
block = blocks[name] = [];
}
block.push(context.fn(this));
});
hbs.registerHelper('block', function(name) {
var val = (blocks[name] || []).join('\n');
// clear the block
blocks[name] = [];
return val;
});
// start the game
iu.init( io );
exports.app = server;