This repository has been archived on 2023-03-26. You can view files and clone it, but cannot push or open issues or pull requests.
tstatic/server.js

67 lines
1.6 KiB
JavaScript
Raw Normal View History

2017-01-11 08:51:50 +00:00
#!/usr/bin/env node
2016-05-16 08:37:24 +01:00
const express = require('express');
2016-08-20 23:09:04 +01:00
const staticFile = require('connect-static-file');
2016-08-26 12:37:06 +01:00
const compression = require('compression');
2016-10-02 11:57:04 +01:00
const helmet = require('helmet');
2016-08-20 23:09:04 +01:00
const path = require('path');
2016-10-30 12:02:53 +00:00
const winston = require('winston');
const expressWinston = require('express-winston');
2017-01-12 17:19:42 +00:00
const opbeat = require('opbeat').start({
active: process.env.NODE_ENV === 'production'
});
2016-05-16 08:37:24 +01:00
2016-08-26 12:37:36 +01:00
const PORT = process.env.PORT || 5000;
2017-01-09 20:42:28 +00:00
let SERVE_DIR;
if (process.env.NODE_ENV === 'test') {
SERVE_DIR = 'site';
} else {
SERVE_DIR = process.argv[process.argv.length - 1];
}
2016-08-20 23:09:04 +01:00
const PAGE_404 = path.join(SERVE_DIR, '.404.html');
2016-05-16 08:37:24 +01:00
const app = express();
2017-01-09 20:34:16 +00:00
app.use(expressWinston.logger({
transports: [
new winston.transports.Console({
colorize: true
})
],
meta: false,
msg: '{{ req.url }} '
.concat('status:{{ res.statusCode }} ')
.concat('useragent:{{ req.headers["user-agent"] }} ')
.concat('time:{{ res.responseTime }}ms'),
colorize: true,
statusLevels: true
}));
2016-08-20 23:09:04 +01:00
2016-05-16 13:07:18 +01:00
app.use(function (request, response, next) {
2016-10-30 12:02:53 +00:00
if (request.url.endsWith('/')) {
2016-08-20 23:09:04 +01:00
request.url = path.join(request.url, 'index.html');
2016-05-16 13:07:18 +01:00
}
next();
});
2017-01-09 20:34:16 +00:00
app.use(express.static(SERVE_DIR, {
dotfiles: 'ignore',
index: false,
redirect: true
}));
2016-08-20 23:09:04 +01:00
app.use(function (request, response, next) {
response.statusCode = 404;
staticFile(PAGE_404)(request, response, next);
});
2016-05-16 08:37:24 +01:00
2016-05-16 13:07:18 +01:00
2017-01-12 17:19:42 +00:00
app.use(compression({ level: 9 }));
app.use(helmet());
app.use(opbeat.middleware.express());
2016-05-16 08:37:24 +01:00
const server = app.listen(PORT, function () {
2017-01-09 20:34:16 +00:00
console.log('Server started on port ' + server.address().port);
2016-05-16 08:37:24 +01:00
});
2016-10-30 22:21:10 +00:00
module.exports = server;