-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
47 lines (40 loc) · 1.17 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
const fs = require('fs');
const express = require('express');
const path = require('path');
let i = 0;
let length = 0;
const walk = (directory, router, debug) => {
const items = fs.readdirSync(directory);
length += items.length;
for (const item of items) {
i++;
const itemPath = path.join(directory, item);
const stat = fs.statSync(itemPath);
if(debug) console.info("Loading route " + item + " (" + i + "/" + length + ")");
if (stat.isFile() && !(item.includes(".js") || item.includes(".ts"))) {
if(debug) console.info(item + " not right file termination");
continue;
} else {
let itemName = item.split(".")[0];
let routePath;
if (itemName == 'index') {
routePath = '/'
} else if (itemName[0] == '_') {
routePath = `/:${itemName.substr(1)}`
} else {
routePath = `/${itemName}`
}
if (stat.isFile()) {
router.use(routePath, require(path.resolve(itemPath)))
} else if (stat.isDirectory()) {
router.use(routePath, middleware(itemPath))
}
}
}
}
const middleware = (directory, debug) => {
const router = express.Router({ mergeParams: true });
walk(directory, router, debug);
return router;
}
module.exports = middleware;