From 2fd2151f63a81a1893de3f42bf954bb4e28cb568 Mon Sep 17 00:00:00 2001 From: ChrisChrome Date: Sun, 25 Jan 2026 14:50:34 -0700 Subject: [PATCH] Load routes properly. its been a minute --- index.js | 23 +++++++++++++---------- routes/api/heartbeat.js | 4 +++- routes/api/shutdown.js | 4 +++- routes/api/startup.js | 4 +++- 4 files changed, 22 insertions(+), 13 deletions(-) diff --git a/index.js b/index.js index beb12d8..6cdb4b5 100644 --- a/index.js +++ b/index.js @@ -34,26 +34,29 @@ app.use((req, res, next) => { next(); }) -// Load routes dynamically from routes/** +// Load routes from routes/* recursively const fs = require('fs'); const path = require('path'); -const routesPath = path.join(__dirname, 'routes'); -// Load recursively function loadRoutes(dir) { fs.readdirSync(dir).forEach(file => { const fullPath = path.join(dir, file); - if (fs.lstatSync(fullPath).isDirectory()) { + const stat = fs.statSync(fullPath); + if (stat.isDirectory()) { loadRoutes(fullPath); - } else if (file.endsWith('.js')) { + } else if (stat.isFile() && file.endsWith('.js')) { const route = require(fullPath); - if (route.path && route.router) { - app.use(route.path, route.router); - console.log(`Loaded route: ${route.path} from ${fullPath}`); - } + // Derive the route path from the file path + const routePath = '/' + path.relative(path.join(__dirname, 'routes'), fullPath) + .replace(/\\/g, '/') // Windows compatibility + .replace('.js', '') + .replace(/index$/,''); + app.use(routePath, route); + console.log(`Loaded route: ${routePath} from ${fullPath}`); } }); } -loadRoutes(routesPath); + +loadRoutes(path.join(__dirname, 'routes')); app.listen(port, () => { console.log(`Listening on ${port}`); diff --git a/routes/api/heartbeat.js b/routes/api/heartbeat.js index bca63b8..13a46e1 100644 --- a/routes/api/heartbeat.js +++ b/routes/api/heartbeat.js @@ -18,4 +18,6 @@ router.post('/', async (req, res) => { return res.status(200).json({ message: 'Heartbeat data recorded' }); } ); -}); \ No newline at end of file +}); + +module.exports = router; \ No newline at end of file diff --git a/routes/api/shutdown.js b/routes/api/shutdown.js index 89a402f..cde475f 100644 --- a/routes/api/shutdown.js +++ b/routes/api/shutdown.js @@ -17,4 +17,6 @@ router.post('/', async (req, res) => { return res.status(200).json({ message: 'Shutdown data recorded' }); } ); -}); \ No newline at end of file +}); + +module.exports = router; \ No newline at end of file diff --git a/routes/api/startup.js b/routes/api/startup.js index 1b035cf..f278239 100644 --- a/routes/api/startup.js +++ b/routes/api/startup.js @@ -19,4 +19,6 @@ router.post('/', async (req, res) => { return res.status(200).json({ message: 'Startup data recorded' }); } ) -}); \ No newline at end of file +}); + +module.exports = router; \ No newline at end of file