How to get the next route route in the Express application

I intercept all traffic before passing it using:

app.all('*', function(req, res, next) {
    ... run before stuff, related to the next req.route.path
    next();
});

and I want to run the code before calling the function next(). so that I know the correct code that I need to execute, I must determine what is the next route of the request route.

Debugging the current req object (inside of everything ('*', ..) gives no information about the following request.route.path

How can I get the following route.path method before calling it?

Your help will be appreciated. Thanks.

+4
source share
2 answers

, , , ?

var middleware = function (req, res, next) {
  ..run your code in here
};

app.get('/users:user_id', middleware, function(req, res, next) {

}); 
+4

, , finish:

app.all('*', function(req, res, next) {
  res.on('finish', function() {
    console.log('Next route: ', req.route.path);
  });
  next();
});

, :

app.get('/users/:user_id', function(req, res) {
  res.send('Hello');
});

:

$ : '/users/: user_id'

+2

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


All Articles