-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
65 lines (58 loc) · 1.87 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
// Load in dependencies
var _ = require('underscore');
var express = require('express');
var expressPartials = require('express-partials');
var ejsEngine = require('consolidate').ejs;
var jadeEngine = require('jade').__express;
var controllers = require('./controllers');
var routes = require('./routes');
// Define a server constructor
function Server(config) {
// Create an app and save config for bindings/later
this.app = express();
this.config = config;
// Register common items
this.registerViewEngine();
this.addRoutes();
}
Server.prototype = {
registerViewEngine: function () {
// Set up view engine and static files for pages
var app = this.app;
app.engine('ejs', ejsEngine);
app.engine('jade', jadeEngine);
app.set('view engine', 'ejs');
app.set('views', __dirname + '/views');
app.use('/public', express['static'](__dirname + '/../dist'));
app.use('/public', express['static'](__dirname + '/../public'));
app.use(expressPartials());
},
addRoutes: function () {
// Localize app and config
var app = this.app;
var config = this.config;
// Extend app.locals with config's app.locals
_.extend(app.locals, config['app.locals']);
// Add test and development specific routes
if (config.addDevelopmentRoutes) {
// Host /test for kaleidoscope
app.use('/test', express['static'](__dirname + '/../test'));
app.use(routes.development(config));
}
if (config.addTestRoutes) {
app.use(routes.test(config));
}
// Bind routes
app.use(routes.common(config));
// Handle middleware errors
app.use(controllers['error-handlers'].errorEncountered(config));
},
listen: function () {
this._app = this.app.listen(this.config.url.internal.port);
},
destroy: function (cb) {
this._app.close(cb || function noop () {});
}
};
// Export the server
module.exports = Server;