1
0
mirror of https://github.com/matt-fidd/stratos.git synced 2026-01-01 23:19:29 +00:00

Refactor loadRoutes

This commit is contained in:
2022-04-18 14:59:41 +00:00
parent 5cbd2831d1
commit 9f9354fcb1

32
app.js
View File

@@ -21,23 +21,25 @@ const importJSON = require('./lib/importJSON');
* @return {Array<Array<String, Object>>} Map of all of the routes * @return {Array<Array<String, Object>>} Map of all of the routes
*/ */
function loadRoutes() { function loadRoutes() {
// Load route files return fs.readdirSync(path.join(__dirname, 'routes'))
const routes = []; .filter(file => file.endsWith('.js'))
.map(file => {
const route = require(path.join(
__dirname,
'routes',
file
));
const routeFiles = return {
fs.readdirSync(path.join(__dirname, 'routes')) ...route,
.filter(file => file.endsWith('.js')); filename: file
};
for (const file of routeFiles) { })
const route = require(path.join(__dirname, 'routes', file)); .sort((a, b) => {
routes.push([ route.root, route.router, route.priority, file ]); if (a.priority > b.priority || b.priority == null)
}
return routes.sort((a, b) => {
if (a[2] > b[2] || b[2] == null)
return 1; return 1;
if (a[2] < b[2] || a[2] == null) if (a.priority < b.priority || a.priority == null)
return -1; return -1;
return 0; return 0;
@@ -134,7 +136,7 @@ async function main() {
}); });
for (const route of loadRoutes()) for (const route of loadRoutes())
app.use(route[0], route[1]); app.use(route.root, route.router);
/* /*
* If the request gets to the bottom of the route stack, it doesn't * If the request gets to the bottom of the route stack, it doesn't