fix 404
This commit is contained in:
parent
2dfa300d7d
commit
051b6fccd3
2 changed files with 14 additions and 12 deletions
|
@ -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": {
|
||||||
|
|
25
server.js
25
server.js
|
@ -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);
|
||||||
|
|
Reference in a new issue