This repository has been archived by the owner on Oct 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
117 lines (102 loc) · 3.74 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
(function() {
'use strict';
var express = require('express');
var path = require('path');
var swig = require('swig');
var bodyParser = require('body-parser')();
var compress = require('compression')();
var serveStatic = require('serve-static');
var errorHandler = require('errorhandler');
// Database
var mongo = require('mongoskin');
var db = mongo.db('mongodb://localhost:27017/woxuexi', {native_parser: true});
var app = express();
var routes = require('./routes/index');
var words = require('./routes/words');
// template
app.engine('.html', swig.renderFile);
app.set('view engine', 'jade');
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);
app.use(require('less-middleware')(path.join(__dirname, 'public')));
// gzip
app.use(compress);
// static files, cached and expire in 30 days
// change path / to /public if need dynamic web
app.use('/', 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']});
// Send Mongo interface to routes
app.use(function(req, res, next) {
req.db = db;
next();
});
// REGISTER OUR ROUTES
app.use('/', routes);
app.use('/words', words);
// 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);
});*/
// error, enable for debugging
app.use(errorHandler({
dumpExceptions: true,
showStack: true
}));
/**
* 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 || 8000;
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);
});
}());