-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
56 lines (50 loc) · 1.49 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
let Express = require("express");
let fs = require("fs");
let path = require("path");
module.exports = (options) => {
options = options || {};
options.vars = options.vars || {};
let log = (...msgs) => {
if (options.logging) {
console.log(...msgs);
}
};
let router = Express.Router();
router.get("*", (req, res, next) => {
let url = req.originalUrl;
let render = file => res.render(file, options.vars);
// Normalize ext/dir
dir = options.dir || req.app.get("views") || "./views";
if (!path.isAbsolute(dir)) {
dir = path.normalize(`./${dir}`);
}
ext = options.ext || req.app.get("view engine") || "pug";
if (ext.startsWith(".")) { ext = ext.substring(1); }
// Render index
if (["/", "/index", "/index.html"].includes(url)) {
log("Rendering index");
render("./index");
return;
}
// Strip leading and trailing /, and remove .html from URLs
if (url.charAt(0) === "/") {
url = url.substring(1);
}
if (url.charAt(url.length - 1) === "/") {
url = url.substring(0, url.length - 1);
} else if (url.substring(url.length - ".html".length) === ".html") {
url = url.substring(0, url.length - ".html".length);
}
// Check if file exists or index file exists in dir
if (fs.existsSync(path.normalize(`${dir}/${url}.${ext}`))) {
log("Rendering " + url);
render(url);
} else if (fs.existsSync(path.normalize(`${dir}/${url}/index.${ext}`))) {
log("Rendering " + url + "/index");
render(`${url}/index`);
} else {
next();
}
});
return router;
};