This commit is contained in:
Jake Howard 2016-08-20 23:09:04 +01:00
parent 2dfa300d7d
commit 051b6fccd3
Signed by: jake
GPG key ID: 57AFB45680EDD477
2 changed files with 14 additions and 12 deletions

View file

@ -20,6 +20,7 @@
}, },
"homepage": "https://github.com/RealOrangeOne/host-container#readme", "homepage": "https://github.com/RealOrangeOne/host-container#readme",
"dependencies": { "dependencies": {
"connect-static-file": "=1.1.2",
"express": "=4.14.0" "express": "=4.14.0"
}, },
"devDependencies": { "devDependencies": {

View file

@ -1,10 +1,12 @@
const express = require('express'); const express = require('express');
const staticFile = require('connect-static-file');
const path = require('path');
const PORT = process.env.PORT; const PORT = 5000;
const serveDir = __dirname + '/site'; const SERVE_DIR = path.join(__dirname, '/site');
const PAGE_404 = path.join(SERVE_DIR, '.404.html');
const directory = /\/$/; const directory = /\/$/;
const allFiles = /.*/;
const expressConfig = { const expressConfig = {
dotfiles: 'ignore', dotfiles: 'ignore',
@ -14,22 +16,21 @@ const expressConfig = {
const app = express(); const app = express();
app.use(function (request, response, next) { app.use(function (request, response, next) {
// If path is directory then serve index.html
if (directory.exec(request.url)) { if (directory.exec(request.url)) {
request.url += '/index.html'; request.url = path.join(request.url, 'index.html');
} }
next(); next();
}); });
app.use( app.use(express.static(SERVE_DIR, expressConfig));
express.static(serveDir, expressConfig)
); app.use(function (request, response, next) {
response.statusCode = 404;
staticFile(PAGE_404)(request, response, next);
});
// Cannot find any file
app.use(
allFiles, express.static(serveDir + '/.404.html', expressConfig)
);
const server = app.listen(PORT, function () { const server = app.listen(PORT, function () {
console.log('Server started on port ' + server.address().port); console.log('Server started on port ' + server.address().port);