unpkg/modules/createServer.js

94 lines
2.2 KiB
JavaScript
Raw Normal View History

2018-12-17 17:38:05 +00:00
const http = require('http');
const express = require('express');
const morgan = require('morgan');
const raven = require('raven');
2017-11-25 21:25:01 +00:00
2018-12-17 17:38:05 +00:00
const staticAssets = require('./middleware/staticAssets');
const createRouter = require('./createRouter');
2017-11-25 21:25:01 +00:00
2018-12-17 17:38:05 +00:00
morgan.token('fwd', req => {
const fwd = req.get('x-forwarded-for');
return fwd ? fwd.replace(/\s/g, '') : '-';
2018-02-17 00:00:06 +00:00
});
2018-08-25 22:13:55 +00:00
if (process.env.SENTRY_DSN) {
raven
.config(process.env.SENTRY_DSN, {
release: process.env.HEROKU_RELEASE_VERSION,
autoBreadcrumbs: true
})
.install();
}
2017-11-10 07:04:43 +00:00
2018-08-25 22:13:55 +00:00
// function errorHandler(err, req, res, next) {
// console.error(err.stack);
2017-11-10 07:04:43 +00:00
2018-08-25 22:13:55 +00:00
// res
// .status(500)
// .type("text")
// .send("Internal Server Error");
// next(err);
// }
2018-02-17 00:00:06 +00:00
function createServer(publicDir, statsFile) {
const app = express();
2018-12-17 17:38:05 +00:00
app.disable('x-powered-by');
2018-08-25 22:13:55 +00:00
if (process.env.SENTRY_DSN) {
app.use(raven.requestHandler());
}
2018-12-17 17:38:05 +00:00
if (process.env.NODE_ENV !== 'test') {
2017-10-20 12:01:10 +00:00
app.use(
morgan(
2018-05-19 15:34:36 +00:00
// Modified version of Heroku's log format
2018-02-17 00:00:06 +00:00
// 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]'
2017-10-20 12:01:10 +00:00
)
2018-02-17 00:00:06 +00:00
);
2017-08-19 00:32:57 +00:00
}
2017-05-25 04:38:06 +00:00
2018-08-25 22:13:55 +00:00
// app.use(errorHandler);
2018-04-29 01:21:23 +00:00
if (publicDir) {
2018-12-17 17:38:05 +00:00
app.use(express.static(publicDir, { maxAge: '365d' }));
2018-04-29 01:21:23 +00:00
}
if (statsFile) {
app.use(staticAssets(statsFile));
}
2018-02-17 00:00:06 +00:00
app.use(createRouter());
2018-08-25 22:13:55 +00:00
if (process.env.SENTRY_DSN) {
app.use(raven.errorHandler());
}
2018-02-17 00:00:06 +00:00
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
2018-08-26 01:19:50 +00:00
server.setTimeout(25000, socket => {
const message = `Timeout of 25 seconds exceeded`;
socket.end(
[
2018-12-17 17:38:05 +00:00
'HTTP/1.1 503 Service Unavailable',
'Date: ' + new Date().toGMTString(),
'Content-Length: ' + Buffer.byteLength(message),
'Content-Type: text/plain',
'Connection: close',
'',
2018-08-26 01:19:50 +00:00
message
2018-12-17 17:38:05 +00:00
].join('\r\n')
2018-08-26 01:19:50 +00:00
);
});
2018-02-17 00:00:06 +00:00
return server;
}
2018-02-17 00:00:06 +00:00
module.exports = createServer;