How to add an express route if the application is already listening?

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.

+5
source share
2 answers

I'm so stupid.

Express 4 by default is able to add a route even after listening to it

So why can't I do this before? because on top of the router level stack I add a stack of error handling levels, so every router level added after it will not be available on request, because when the request is processed, it will first catch the error handler layer.

therefore, the correct method is as follows:

  • I need to control which index is the stack level of the error handler located in app._router.stack , in this case it is some layer at the very end of the array

  • Add a new route, for example: app.use("/something", function(req, res, next){ res.send("Lol") })

  • Delete the error handler layer stacks and place it at the very end of the router stack array

    // in this case, error map is array // contain index location of error handling stack layer var error_handlers = app._router.stack.splice(error_map[0], error_map.length); app._router.stack.push.apply(app._router.stack, error_handlers);

Now you are ready to go.

+1
source

After scrolling with express code, I found the following:

 router.get('/', function(req, res) { res.render('index', { title: 'Express' }); console.log("adding route") addGet('/mypath', function(req, res) { res.send('hi') }); }); function addGet(path, callback) { Router = require('express').router; // We get a layer sample so we can instatiate one after layerSample = router.stack[0]; // get constructors Layer = layerSample.constructor; Route = layerSample.route.constructor; // see https://github.com/strongloop/express/blob/4.x/lib/router/index.js#L457 route = new Route(path); var layer = new Layer(path, { sensitive: this.caseSensitive, strict: this.strict, end: true }, route.dispatch.bind(route)); layer.route = route; // And we bind get route.get(callback) // And we map it router.stack.push(layer); } 

So, open a browser in localhost , and then in localhost/mypath it works !!!

+1
source

Source: https://habr.com/ru/post/1201213/


All Articles