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

64 lines
1.6 KiB
JavaScript
Raw Normal View History

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');
2016-05-16 08:37:24 +01:00
2016-10-30 22:21:10 +00:00
const IN_TEST = process.env.NODE_ENV === 'true';
2016-08-26 12:37:36 +01:00
const PORT = process.env.PORT || 5000;
2016-08-20 23:09:04 +01:00
const SERVE_DIR = path.join(__dirname, '/site');
const PAGE_404 = path.join(SERVE_DIR, '.404.html');
2016-10-30 12:02:53 +00:00
const EXPRESS_CONFIG = {
2016-08-13 16:31:22 +01:00
dotfiles: 'ignore',
2016-05-16 08:37:24 +01:00
index: false,
redirect: true
};
2016-10-30 12:02:53 +00:00
const LOGGER_MESSAGE = '{{ req.url }}'
.concat('status:{{ res.statusCode }} ')
.concat('useragent:{{ req.headers["user-agent"] }} ')
.concat('time:{{ res.responseTime }}ms');
2016-05-16 08:37:24 +01:00
const app = express();
2016-08-26 12:37:06 +01:00
app.use(compression({ level: 9 }));
2016-10-02 11:57:04 +01:00
app.use(helmet());
2016-10-30 22:21:10 +00:00
if (IN_TEST) {
console.log('Enabling Logging...');
app.use(expressWinston.logger({
transports: [
new winston.transports.Console({
colorize: true
})
],
meta: false,
msg: LOGGER_MESSAGE,
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();
});
2016-10-30 12:02:53 +00:00
app.use(express.static(SERVE_DIR, EXPRESS_CONFIG));
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
2016-05-16 08:37:24 +01:00
const server = app.listen(PORT, function () {
2016-10-30 22:21:10 +00:00
if (IN_TEST) {
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;