From 051b6fccd3c6a313b19fec4c11470af9956a0e7d Mon Sep 17 00:00:00 2001 From: TheOrangeOne Date: Sat, 20 Aug 2016 23:09:04 +0100 Subject: [PATCH] fix 404 --- package.json | 1 + server.js | 25 +++++++++++++------------ 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index df70f4d..e565841 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ }, "homepage": "https://github.com/RealOrangeOne/host-container#readme", "dependencies": { + "connect-static-file": "=1.1.2", "express": "=4.14.0" }, "devDependencies": { diff --git a/server.js b/server.js index 3e8d344..5ff85ae 100644 --- a/server.js +++ b/server.js @@ -1,10 +1,12 @@ const express = require('express'); +const staticFile = require('connect-static-file'); +const path = require('path'); -const PORT = process.env.PORT; -const serveDir = __dirname + '/site'; +const PORT = 5000; +const SERVE_DIR = path.join(__dirname, '/site'); +const PAGE_404 = path.join(SERVE_DIR, '.404.html'); const directory = /\/$/; -const allFiles = /.*/; const expressConfig = { dotfiles: 'ignore', @@ -14,22 +16,21 @@ const expressConfig = { const app = express(); + app.use(function (request, response, next) { - // If path is directory then serve index.html if (directory.exec(request.url)) { - request.url += '/index.html'; + request.url = path.join(request.url, 'index.html'); } next(); }); -app.use( - express.static(serveDir, expressConfig) -); +app.use(express.static(SERVE_DIR, 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 () { console.log('Server started on port ' + server.address().port);