legendaryjs
Version:
LegendaryJS – The ultimate backend framework for speed, power, and simplicity.
34 lines (27 loc) • 1.08 kB
JavaScript
const fs = require('fs');
const path = require('path');
function createRoute(app, options) {
const { method = 'get', path: routePath, handler } = options;
if (!method || !routePath || !handler) {
console.error('❌ createRoute requires method, path, and handler.');
return;
}
app[method.toLowerCase()](routePath, async (req, res) => {
try {
const result = await handler(req, res);
res.json(result);
} catch (err) {
res.status(500).json({ error: err.message || 'Internal Server Error' });
}
});
// Optionally add to routes file (visual feedback only)
const routesFile = path.join(process.cwd(), 'routes/api.v1.json');
if (fs.existsSync(routesFile)) {
const content = JSON.parse(fs.readFileSync(routesFile));
content.routes = content.routes || [];
content.routes.push({ method, path: routePath });
fs.writeFileSync(routesFile, JSON.stringify(content, null, 2));
}
console.log(`📦 Route [${method.toUpperCase()} ${routePath}] created`);
}
module.exports = { createRoute };