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

43 lines
981 B
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-05-16 08:37:24 +01:00
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-05-16 13:07:18 +01:00
const directory = /\/$/;
2016-05-16 08:37:24 +01:00
const expressConfig = {
2016-08-13 16:31:22 +01:00
dotfiles: 'ignore',
2016-05-16 08:37:24 +01:00
index: false,
redirect: true
};
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-08-26 12:37:06 +01:00
2016-08-20 23:09:04 +01:00
2016-05-16 13:07:18 +01:00
app.use(function (request, response, next) {
if (directory.exec(request.url)) {
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-08-20 23:09:04 +01:00
app.use(express.static(SERVE_DIR, expressConfig));
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-08-13 16:31:22 +01:00
console.log('Server started on port ' + server.address().port);
2016-05-16 08:37:24 +01:00
});