This repository has been archived by the owner on Sep 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
88 lines (74 loc) · 2.13 KB
/
server.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
var express = require('express'),
app = express(),
swig = require('swig'),
// Middleware
morgan = require('morgan'),
compression = require('compression'),
bodyParser = require('body-parser'),
// Helpers
path = require('path'),
config = require('./config'),
devMode = config.env === 'development';
app.use(compression());
swig.setDefaults({
cache: devMode ? false : 'memory',
locals: {
now: function() {
return new Date();
},
appName: config.appName
},
varControls: ['{=', '=}']
});
if (devMode) {
app.use(morgan('dev'));
}
app.use(bodyParser.json());
// Set up templating engine
app.engine('html', swig.renderFile);
app.set('view engine', 'html');
app.set('views', path.join(config.root, 'server/views'));
// Public files
app.use(express.static(path.join(config.root, 'public')));
app.use(express.static(path.join(config.root, 'public/bower')));
app.use(express.static(path.join(config.root, 'public/css')));
app.use(express.static(path.join(config.root, 'public/js')));
app.use(express.static(path.join(config.root, 'public/partials')));
// Path router
var routes = require('./routes');
for (var route in routes) {
routes.hasOwnProperty(route) && bindRoute(route);
}
function bindRoute (route) {
var page = routes[route];
app.route(route).get(function (req, res) {
res.render(page.view, {data: JSON.stringify(page.data)});
});
console.log(path.normalize(route + '/update'))
app.route(path.normalize(route + '/update')).get(function (req, res) {
res.send(page.data);
});
}
// Create a 404 page
app.route('*/update').all(function(req, res) {
res.send({
title: '404',
assets: {
js: [],
css: ['normalize', 'main']
}
});
});
app.route('*').all(function(req, res) {
res.render('404', {
title: '404',
assets: {
js: [],
css: ['normalize', 'main']
}
});
});
// Open the ports for business
app.listen(config.port, function() {
console.log('%s running on port %d in %s mode', config.appName, config.port, config.env);
});