-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
121 lines (102 loc) · 3.33 KB
/
Copy pathserver.js
File metadata and controls
121 lines (102 loc) · 3.33 KB
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
var express = require('express')
, versionator = require('versionator')
, _ = require('lodash')
, mappedVersion = versionator.createMapped(require(__dirname + '/static-file-map.json'))
module.exports = function (serviceLocator, routes) {
var app = express()
, gracefullyExiting = false
// One month static file expire on non-development
, staticContentExpiry =
serviceLocator.properties.env === 'development' ? 0 : 2592000000
// App and router might not always be the same thing as app
serviceLocator
.register('app', app)
.register('router', app)
// Return 502 while the server is shutting down
app.use(function (req, res, next) {
if (!gracefullyExiting) {
return next()
}
res.set('Connection', 'close')
res.send(502, 'Server is in the process of restarting.')
})
// Taken from: http://blog.argteam.com/
process.on('SIGTERM', function () {
gracefullyExiting = true
serviceLocator.logger.warn('Received kill signal (SIGTERM), shutting down')
setTimeout(function () {
serviceLocator.logger.error('Could not close connections in time, forcefully shutting down')
process.exit(1)
}, 30000)
serviceLocator.server.close(function () {
serviceLocator.logger.info('Closed out remaining connections.')
process.exit()
})
})
app.set('view engine', 'jade')
app.set('views', __dirname + '/views/templates')
// Wire up the express logger to the app logger
app.use(express.logger(
{ format: 'dev'
, stream:
{ write: function (data) {
serviceLocator.logger.info((data + '').trim())
}
}
}))
function compressFilter(req, res) {
return (/svg|json|text|javascript/).test(res.getHeader('Content-Type'))
}
app
.use(express.responseTime())
.use(mappedVersion.middleware)
// Gzip Compression for static assets
.use('/static', express.compress({ filter: compressFilter }))
.use('/static', express.static(__dirname + '/public',
{ maxAge: staticContentExpiry }))
.use(function (req, res, next) {
// Don't cache article and section previews
if (req.query.previewId) {
res.set(
{ 'Cache-Control': 'max-age=0'
, 'Pragma': 'no-cache'
, 'Expires': 0
})
} else {
// Set a little bit of cache to stop reduce load
res.set(
{ 'Cache-Control': 'max-age=60'
})
}
next()
})
// Make versionPath available in the jade templates
.use(function (req, res, next) {
res.locals.versionPath = mappedVersion.versionPath
next()
})
// Redirect urls with a trailing slash
app.use(function (req, res, next) {
if (/\w+\/$/.test(req.url)) {
res.redirect(req.url.substr(0, req.url.length - 1))
} else {
next()
}
})
routes(serviceLocator)
/* jshint unused: false */
app.use(function (req, res, next) {
res.status(404)
res.render('error/404')
})
app.use(function errorHandler(error, req, res, next) {
serviceLocator.logger.error('Error occurred while handling request:\n',
_.pick(req, 'method', 'url', 'query', 'headers', 'ip', 'ips'))
serviceLocator.logger.error(error.message)
serviceLocator.logger.error(error.stack)
var status = error.status || 500
res.status(status)
res.render('error/' + status)
})
return app
}