-
Notifications
You must be signed in to change notification settings - Fork 9
/
app.js
151 lines (124 loc) · 4.46 KB
/
app.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/**
* Module dependencies.
*/
var settings = require('./settings');
// Create an express application
var express = require('express');
var app = express();
var server = require('http').Server(app);
// Set environment mode
var environment = settings.environment;
app.set('env', process.env.NODE_ENV || environment);
// Set server port
app.set('port', process.env.PORT || settings[environment].port);
// Setup compression middleware
app.use(require('compression')());
// Setup body parser middleware
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
// Setup file uploading middleware
app.use(require('./models/oss').upload());
// Setup method override middleware
app.use(require('method-override')());
// Setup cookie parsing middleware
var cookieParser = require('cookie-parser');
var secret = settings.cookie.secret;
app.use(cookieParser(secret));
// Setup simple cookie-based session middleware
app.use(require('cookie-session')(settings.cookie));
// Setup CSRF protection middleware
app.use(require('csurf')());
// Setup i18n simple translation middleware
var i18n = require('i18n');
i18n.configure(settings.locale);
app.use(i18n.init);
// Add expires headers for static resources
app.use(function (req, res, next) {
if (/\/(images|javascripts|stylesheets)\//.test(req.url)) {
var cache = settings.cache;
res.setHeader('Cache-Control', 'public, max-age=' + cache.maxAge);
res.setHeader('Expires', new Date(cache.expires).toUTCString());
}
next();
});
// Setup favicon serving middleware
var path = require('path');
var faviconPath = path.join(__dirname, 'public/images/favicon.ico');
app.use(require('serve-favicon')(faviconPath));
// Set public directory
var publicDirectory = path.join(__dirname, 'public');
app.use(express.static(publicDirectory));
// Set view directory path
var viewDirectory = path.join(__dirname, 'views');
app.set('views', viewDirectory);
// Set default template engine
app.set('view engine', 'jade');
// Register template engine based on the file extensions
app.engine('jade', function (path, options, callback) {
options.pretty = true;
return require('jade').__express(path, options, callback);
});
// Enable view template compilation caching
if (environment === 'production') {
app.enable('view cache');
}
// Enable reverse proxy support
app.enable('trust proxy');
// Setup logging middleware
app.use(require('morgan')('[:date] :remote-addr [:user-agent] :referrer :method :url :status :response-time ms'));
// Set application local variables provided to all templates
app.locals.appVersion = settings.version;
app.locals.appTitle = settings.title;
app.locals.appKeywords = settings.keywords.join(', ');
app.locals.appDescription = settings.description;
app.locals.appStorage = settings[environment].storage;
// Set router instances
app.use('/', require('./routes/index'));
app.use('/auth', require('./routes/auth'));
app.use('/chat', require('./routes/chat'));
app.use('/users', require('./routes/users'));
app.use('/search', require('./routes/search'));
app.use('/browse', require('./routes/browse'));
app.use('/explore', require('./routes/explore'));
app.use('/terminal', require('./routes/terminal'));
app.use('/articles', require('./routes/articles'));
app.use('/reviews', require('./routes/reviews'));
app.use('/visual', require('./routes/visual'));
app.use('/tools', require('./routes/tools'));
app.use('/admin', require('./routes/admin'));
app.use('/site', require('./routes/site'));
app.use('/help', require('./routes/help'));
app.use('/', require('./routes/errors'));
// Handle application error
app.use(function (err, req, res, next) {
console.error(new Date());
console.error(err);
res.render('500');
});
// Integrate websokect server
require('./models/socket')(server, secret);
// Launch a cluster of Node processes
var cluster = require('cluster');
if (cluster.isMaster) {
var threads = settings[environment].threads;
var numCPUs = Math.min(threads, require('os').cpus().length);
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', function (worker, code, signal) {
console.log('worker ' + worker.process.pid + ' died');
});
// Run job scheduler
require('./models/schedule')(function () {
console.log('scheduled jobs will execution at specific dates');
});
} else {
var port = app.get('port');
server.listen(port, function () {
console.log('Express server listening on port ' + port +
' in ' + app.get('env') + ' mode');
});
}