Browse Source

Allow node server to serve .html files without extension

autofurnace
parent
commit
11b8deba1a
3 changed files with 50 additions and 2 deletions
  1. +43
    -2
      app.js
  2. +5
    -0
      package-lock.json
  3. +2
    -0
      package.json

+ 43
- 2
app.js View File

@@ -1,15 +1,15 @@
// SERVER
var fs = require('fs')
var path = require('path');
var express = require('express');
var serveStatic = require('serve-static');
var mime = require('mime-types')
var argv = require('yargs').argv;
var dest = './build'
if (argv.dest){
dest = argv.dest
}
var app = express().use(serveStatic(dest));
var reload = require('reload')
var reloader = reload(app);

// BUILDER
var config = {
@@ -47,9 +47,50 @@ console.log(config);
builder(config);

if (!argv.b){
var app = express();
app.use(function(req, res, next) {
if (req.path.substr(-1) == '/' && req.path.length > 1) {
let query = req.url.slice(req.path.length);
res.redirect(301, req.path.slice(0, -1) + query);
} else {
next();
}
});
app.get('/', (req, res, next)=>{
fs.readFile(path.join(dest, "index.html"), (err, data)=>{
if (err){
next();
} else {
res.set('Content-Type', 'text/html');
res.send(data);
}
})
})
app.get(/.*/, (req, res, next)=>{
fs.readFile(path.join(dest, req.path), (err, data)=>{
if (err){
next();
} else {
var mtype = mime.lookup(req.path);
res.set('Content-Type', mtype ? mtype : 'text/plain');
res.send(data);
}
})
})
app.get(/.*/, (req, res, next)=>{
fs.readFile(path.join(dest, req.path+".html"), (err, data)=>{
if (err){
next();
} else {
res.set('Content-Type', 'text/html');
res.send(data);
}
})
})
app.listen(3000, function(){
console.log('Server running on 3000...');
});
var reloader = reload(app);
}

//builder(reload, libconfig);

+ 5
- 0
package-lock.json View File

@@ -3148,6 +3148,11 @@
"resolved": "https://registry.npmjs.org/fast-safe-stringify/-/fast-safe-stringify-2.0.7.tgz",
"integrity": "sha512-Utm6CdzT+6xsDk2m8S6uL8VHxNwI6Jub+e9NYTcAms28T84pTa25GJQV9j0CY0N1rM8hK4x6grpF2BQf+2qwVA=="
},
"file-type": {
"version": "12.4.0",
"resolved": "https://registry.npmjs.org/file-type/-/file-type-12.4.0.tgz",
"integrity": "sha512-WTvyKq8yjtNmUtVAD8LGcTkvtCdJglM6ks2HTqEClm6+65XTqM6MoZYA1Vtra50DLRWLiM38fEs1y56f5VhnUA=="
},
"filename-regex": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/filename-regex/-/filename-regex-2.0.1.tgz",


+ 2
- 0
package.json View File

@@ -19,6 +19,7 @@
"dateformat": "^3.0.3",
"del": "^3.0.0",
"express": "^4.16.3",
"file-type": "^12.4.0",
"flat-map": "^1.0.0",
"gulp": "^4.0.0",
"gulp-change": "^1.0.2",
@@ -53,6 +54,7 @@
"markdown-it": "^8.4.2",
"markdown-it-include": "^1.1.0",
"marked": "^0.6.1",
"mime-types": "^2.1.25",
"output-file": "^2.0.0",
"plugin-error": "^1.0.1",
"reload": "^2.2.2",


Loading…
Cancel
Save