forked from balderdashy/sails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinitialize.js
122 lines (88 loc) · 4.39 KB
/
initialize.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**
* Module dependencies.
*/
var _ = require('lodash'),
async = require('async');
module.exports = function(sails) {
/**
* Configure the encapsulated Express server that will be used to serve actual HTTP requests
*/
return function loadExpress(cb) {
// In order for an existent and correct `sails.config`
// to be passed in, these requires should be here,
// not above: ||
// \/
var startServer = require('./start')(sails);
// Provides support for undocumented `express.loadMiddleware` config
// (should no longer be relevant in most cases thanks to new `http.middleware` conf)
var installHTTPMiddleware = sails.config.http.loadMiddleware || require('./middleware/load');
// Required to be here due to dynamic NODE_ENV settings via command line args
// (i.e. if we `require` this above w/ everything else, the NODE_ENV might not be set properly yet)
var express = require('express');
// Create express server
var app = sails.hooks.http.app = express();
// (required by Express 3.x)
var usingSSL = sails.config.ssl.key && sails.config.ssl.cert;
// Merge SSL into server options
var serverOptions = sails.config.http.serverOptions || {};
_.extend(serverOptions, sails.config.ssl);
// Get the appropriate server creation method for the protocol
var createServer = usingSSL ?
require('https').createServer :
require('http').createServer;
// Use serverOptions if they were specified
// Manually create http server using Express app instance
if (sails.config.http.serverOptions || usingSSL) {
sails.hooks.http.server = createServer(serverOptions, sails.hooks.http.app);
} else sails.hooks.http.server = createServer(sails.hooks.http.app);
// Configure views if hook enabled
if (sails.hooks.views) {
sails.after('hook:views:loaded', function() {
var View = require('./view');
// Use View subclass to allow case-insensitive view lookups
sails.hooks.http.app.set('view', View);
// Set up location of server-side views and their engine
sails.hooks.http.app.set('views', sails.config.paths.views);
// Teach Express how to render templates w/ our configured view extension
app.engine(sails.config.views.engine.ext, sails.config.views.engine.fn);
// Set default view engine
sails.log.verbose('Setting default Express view engine to ' + sails.config.views.engine.ext + '...');
sails.hooks.http.app.set('view engine', sails.config.views.engine.ext);
});
}
// When Sails binds routes, bind them to the internal Express router
sails.on('router:bind', function(route) {
route = _.cloneDeep(route);
// TODO: Add support for error domains..?
app[route.verb || 'all'](route.path, route.target);
});
// When Sails unbinds routes, remove them from the internal Express router
sails.on('router:unbind', function(route) {
var newRoutes = [];
_.each(app.routes[route.method], function(expressRoute) {
if (expressRoute.path != route.path) {
newRoutes.push(expressRoute);
}
});
app.routes[route.method] = newRoutes;
});
// in case Redis session store fails, return 500 immediately
// if session isn't already set, assume it's ok
app.use(function (req, res, next) {
if (sails.config.session.state === false) {
sails.log.error('Session store fail');
return res.send(500); // handle error
}
return next();
});
// When Sails is ready, start the express server
sails.on('ready', startServer);
// app.use() the configured express middleware
var defaultMiddleware = require('./middleware/defaults')(sails, app);
installHTTPMiddleware(app, defaultMiddleware, sails);
// TODO: investigate sharing http middleware with sockets hook..?
// (maybe not-- this is an open question with direct impact on sessions)
// (this would be a v0.11.x thing)
return cb();
};
};