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);