legendaryjs
Version:
LegendaryJS – The ultimate backend framework for speed, power, and simplicity.
25 lines (20 loc) • 873 B
JavaScript
const fs = require('fs');
const path = require('path');
function loadVersionedRoutes(app) {
const routesDir = path.join(process.cwd(), 'routes');
const files = fs.readdirSync(routesDir).filter(f => f.startsWith('api.v') && f.endsWith('.json'));
files.forEach(file => {
const version = file.match(/api\.v(\d+)\.json/)[1];
const data = require(path.join(routesDir, file));
data.routes.forEach(route => {
const method = route.method.toLowerCase();
const pathWithVersion = `/v${version}${route.path}`;
const handler = (req, res) => {
res.json({ version: `v${version}`, data: route.response || null });
};
app[method](pathWithVersion, handler);
console.log(`📦 Loaded [v${version}] ${method.toUpperCase()} ${pathWithVersion}`);
});
});
}
module.exports = { loadVersionedRoutes };