I want to create automatic routing in express, currently I can read the directory and add a route manually from all available files, the added route can also be updated if there are changes in the route file
delete require.cache[require.resolve(scriptpath)]; var routescript = {}; try { routescript = require(scriptpath); } catch (e){ console.log('Express >> Ignoring error route: ' + route + ' ~ >' + scriptpath); } var stack_index = app._router.stack_map[route] var stack = app._router.stack[stack_index]; if (stack) { app._router.stack[stack_index].handle = routescript; console.log('Replace Route Stack \'' + route + '\''); } else { app.use(route, routescript); var stack_index = app._router.stack_map[route] = (app._router.stack.length-1); console.log('Add Route Stack \'' + route + '\''); }
But they only work before the application listens on the port,
How to add / remove a new route stack after listening to the application through the port?
One way I can think of is to close the configure / add / remove server's re listen route, but I think this is bad practice.
source share