-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
99 lines (78 loc) · 2.33 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
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env node
var http = require('http');
var express = require('express');
var bodyParser = require('body-parser');
var argv = require('yargs').argv;
//initializing local modules
var localrequirehandler = require('./core/scaffold/localrequire');
localrequirehandler.init(); //after this function ran, you will be able to use localrequire
//calling polyfills
var polyfillhandler = localrequire('core.scaffold.polyfill-handler');
polyfillhandler.init();
//initializing config manager
var config = localrequire('configManager');
config.init(argv.env);
//creating express app
var app = express();
// configure app to use bodyParser
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true })); //this will let us get the data from a POST
//initializing session handler
var session = localrequire('core.infrastructure.session-manager');
session.init(app);
//initializing resources
var resourcehandler = localrequire('core.scaffold.resourcehandler');
resourcehandler.init(app);
//initiating error handler
var errorHandler = localrequire('core.scaffold.error-handler');
errorHandler.init(app);
/**
* Create HTTP server
*/
var server = http.createServer(app);
var port = process.env.PORT || config.server.port; //some application like heroku provides dynamic port
app.set('port', port);
/**
* listen on provided port
*/
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
/**
* onError
* Listen for HTTP 'error' event
* @param {object} error
*/
function onError(error) {
if(error.syscall !== 'listen') {
throw error;
}
var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
// handle specific errors with friendly messages
switch(error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privilates');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
/**
* onListening
* Listen for http 'listening' event
* @return {undefined}
*/
function onListening() {
var addr = server.address();
var bind = typeof port === 'string'
? 'pipe ' + port
: 'port ' + port;
console.log('Listening on ' + bind);
}