-
Notifications
You must be signed in to change notification settings - Fork 91
/
server.js
119 lines (104 loc) · 3.83 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
(function() {
'use strict';
var express = require('express');
var swig = require('swig');
var bodyParser = require('body-parser');
var compress = require('compression')();
var serveStatic = require('serve-static');
var errorHandler = require('errorhandler');
var cookieParser = require('cookie-parser');
var session = require('express-session');
var flash = require('connect-flash');
var configs = require('./config');
var app = express();
// template
app.engine('.html', swig.renderFile);
app.set('view engine', 'html');
app.set('views', __dirname + '/views');
app.set('view options', {layout: false});
// Swig will cache templates for you, but you can disable
// that and use Express's caching instead, if you like:
app.set('view cache', false);
// To disable Swig's cache, do the following:
swig.setDefaults({cache: false});
// NOTE: You should always cache templates in a production environment.
// Don't leave both of these to `false` in production!
// configure app to use bodyParser()
// this will let us get the data from a POST via
// POST: {"name":"foo","color":"red"} or
// POST: name=foo&color=red
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.use(cookieParser(configs.secret));
app.use(session({
secret: configs.secret,
resave: true,
saveUninitialized: true,
cookie: {maxAge: 60000}
}));
app.use(flash());
// gzip
app.use(compress);
// static files, cached and expire in 30 days
// change path / to /public if need dynamic web
var staticPath = configs.isDynamic ? '/public' : '/';
app.use(staticPath, serveStatic(__dirname + '/public', {maxAge: 2592000000}));
// mime
express.static.mime.define(
{'application/x-web-app-manifest+json': ['webapp']});
express.static.mime.define({'text/cache-manifest': ['appcache']});
express.static.mime.define({'image/x-icon': ['ico']});
// express.static.mime.define({'audio/ogg': ['ogg']});
// express.static.mime.define({'audio/mp4': ['m4a']});
if (configs.isDynamic) {
require('./routes/urls')(app);
}
if (configs.debug) {
app.use(errorHandler({dumpExceptions: true, showStack: true}));
} else {
// Handle 404
app.use(function(req, res) {
res.send('404: Page not Found', 404);
});
// Handle 500
app.use(function(error, req, res, next) {
res.send('500: Internal Server Error', 500);
});
app.use(errorHandler());
}
/**
* terminator === the termination handler
* Terminate server on receipt of the specified signal.
* @param {string} sig Signal to terminate on.
*/
var terminator = function(sig) {
if (typeof sig === 'string') {
console.log('%s: Received %s - terminating sample app ...',
Date(Date.now()), sig);
process.exit(1);
}
console.log('%s: Node server stopped.', Date(Date.now()));
};
// Process on exit and signals.
process.on('exit', function() { terminator(); });
// Removed 'SIGPIPE' from the list - bugz 852598.
['SIGHUP', 'SIGINT', 'SIGQUIT', 'SIGILL', 'SIGTRAP', 'SIGABRT',
'SIGBUS', 'SIGFPE', 'SIGUSR1', 'SIGSEGV', 'SIGUSR2', 'SIGTERM'
].forEach(function(element, index, array) {
process.on(element, function() { terminator(element); });
});
// Set the environment variables we need.
var APP_PORT = process.env.VCAP_APP_PORT ||
process.env.OPENSHIFT_NODEJS_PORT || configs.port;
var APP_IPADDRESS = process.env.OPENSHIFT_NODEJS_IP || '';
if (typeof APP_IPADDRESS === 'undefined') {
// Log errors but continue w/ 127.0.0.1 - this
// allows us to run/test the app locally.
console.warn('No IP address var, using 127.0.0.1');
APP_IPADDRESS = '127.0.0.1';
}
app.listen(APP_PORT, APP_IPADDRESS, function() {
console.log('%s: Node server started on %s:%d ...',
Date(Date.now()), APP_IPADDRESS, APP_PORT);
});
}());