forked from unpkg/unpkg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateServer.js
93 lines (73 loc) · 2.18 KB
/
createServer.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
const http = require('http');
const express = require('express');
const morgan = require('morgan');
const raven = require('raven');
const staticAssets = require('./middleware/staticAssets');
const createRouter = require('./createRouter');
morgan.token('fwd', req => {
const fwd = req.get('x-forwarded-for');
return fwd ? fwd.replace(/\s/g, '') : '-';
});
if (process.env.SENTRY_DSN) {
raven
.config(process.env.SENTRY_DSN, {
release: process.env.HEROKU_RELEASE_VERSION,
autoBreadcrumbs: true
})
.install();
}
// function errorHandler(err, req, res, next) {
// console.error(err.stack);
// res
// .status(500)
// .type("text")
// .send("Internal Server Error");
// next(err);
// }
function createServer(publicDir, statsFile) {
const app = express();
app.disable('x-powered-by');
if (process.env.SENTRY_DSN) {
app.use(raven.requestHandler());
}
if (process.env.NODE_ENV !== 'test') {
app.use(
morgan(
// Modified version of Heroku's log format
// https://devcenter.heroku.com/articles/http-routing#heroku-router-log-format
'method=:method path=":url" host=:req[host] request_id=:req[x-request-id] cf_ray=:req[cf-ray] fwd=:fwd status=:status bytes=:res[content-length]'
)
);
}
// app.use(errorHandler);
if (publicDir) {
app.use(express.static(publicDir, { maxAge: '365d' }));
}
if (statsFile) {
app.use(staticAssets(statsFile));
}
app.use(createRouter());
if (process.env.SENTRY_DSN) {
app.use(raven.errorHandler());
}
const server = http.createServer(app);
// Heroku dynos automatically timeout after 30s. Set our
// own timeout here to force sockets to close before that.
// https://devcenter.heroku.com/articles/request-timeout
server.setTimeout(25000, socket => {
const message = `Timeout of 25 seconds exceeded`;
socket.end(
[
'HTTP/1.1 503 Service Unavailable',
'Date: ' + new Date().toGMTString(),
'Content-Length: ' + Buffer.byteLength(message),
'Content-Type: text/plain',
'Connection: close',
'',
message
].join('\r\n')
);
});
return server;
}
module.exports = createServer;