-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
78 lines (60 loc) · 1.79 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
/**
* Initializes and starts the Express server.
*/
'use strict';
var express = require('express'),
http = require('http'),
passport = require('passport'),
config = require('./config/environment');
global.models = require('./app/models');
var server;
var app = express();
/**
* Basic Express setup for logging, parser, static routes...
*/
require('./config/express')(app, config);
/**
* Sets up passport authentication and Express sessions.
*/
require('./config/authConfig')(app, config, passport);
/**
* Configures Express routes.
*/
require('./config/routes')(app, config, passport);
/**
* Adds error handlers.
*/
require('./config/errorHandler')(app);
/**
* Starts the HTTP server. If in production environment, sets
* uid and gid after start (allows use of lower ports if that's
* needed).
*/
function startServer() {
// stop annoying error message when testing.
if (server !== undefined) {
server.close();
}
// start server
server = http.createServer(app).listen(config.port, function () {
if (config.nodeEnv !== 'development') {
try {
console.log('Old User ID: ' + process.getuid() + ', Old Group ID: ' + process.getgid());
process.setgid(config.gid);
process.setuid(config.uid);
console.log('New User ID: ' + process.getuid() + ', New Group ID: ' + process.getgid());
console.log('Express server listening on port ' + config.port);
} catch (err) {
console.log('Refusing to keep the process alive as root.');
console.log(err);
process.exit(1);
}
} else {
console.log('Running with User Id: ' + process.getuid());
console.log('Express server listening on port ' + config.port);
}
});
}
// This is needed when running from IDE
module.exports = app;
startServer();