1
0
mirror of https://github.com/matt-fidd/stratos.git synced 2026-01-01 20:59:30 +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
*/
function loadRoutes() {
// Load route files
const routes = [];
return fs.readdirSync(path.join(__dirname, 'routes'))
.filter(file => file.endsWith('.js'))
.map(file => {
const route = require(path.join(
__dirname,
'routes',
file
));
const routeFiles =
fs.readdirSync(path.join(__dirname, 'routes'))
.filter(file => file.endsWith('.js'));
for (const file of routeFiles) {
const route = require(path.join(__dirname, 'routes', file));
routes.push([ route.root, route.router, route.priority, file ]);
}
return routes.sort((a, b) => {
if (a[2] > b[2] || b[2] == null)
return {
...route,
filename: file
};
})
.sort((a, b) => {
if (a.priority > b.priority || b.priority == null)
return 1;
if (a[2] < b[2] || a[2] == null)
if (a.priority < b.priority || a.priority == null)
return -1;
return 0;
@@ -134,7 +136,7 @@ async function main() {
});
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