Alexander Mills
Alexander Mills

Reputation: 100060

Jumping to different route chain in Express

I discovered the fairly useful device in Express to jump to a new chain of middleware in Express

say we have this:

router.post('/', function(req,res,next){

   next();

}, function(req,res,next){

   next('route');  //calling this will jump us down to the next router.post call

}, function(req,res,next){ //not invoked

   next();  

}, function(err,req,res,next){

      //definitely not invoked
});



router.post('/', function(req,res,next){  //this gets invoked by the above next('route') call

   next();


}, function(err,req,res,next){

});

I could see where this might be useful and am trying to figure out how it works.

The problem that I see is this solution just seems to kick the can down the road a bit. What I want is to be able to call next('route:a') or next('route:b') so I can select which handler to invoke by name, not just the next one in the list.

As an example, I have this:

router.post('/', function (req, res, next) {  //this is invoked first
    console.log(1);
    next('route');
});

router.post('/', function (req, res, next) {  //this is invoked second
    console.log(2);
    next('route');
});

router.use('foo', function (req, res, next) {  //this gets skipped
    console.log(3);
});

router.post('bar', function (req, res, next) {  //this get skipped
    console.log(4);
});

router.post('/', function(req,res,next){  // this gets invoked third
    console.log(5);
 });  

What I am looking for is a way to invoke "foo" and "bar" by name. Is there a way to do that with Express?

Upvotes: 12

Views: 2491

Answers (2)

Aishwat Singh
Aishwat Singh

Reputation: 4459

Actually next('route') goest to next route but your url is / not foo so it skips that and moves on to next routes, till it finds a matching one , which happens at last case and you get 5 in console

If u want you can just change req.url to something like foo or whatever, then it will enter that route (and it won't skip middleares for that route) or you can do something like res.redirect then call will come again from client

router.post('/', function (req, res, next) {  //this is invoked second
    console.log(2);
    req.url="foo";
    next('route');
});

Actually @zag2art approach is good , At the end of day you have to keep ur code smart enough to handle your cases elegantly. Express doesn't provide anything as such to skip to a particualr route

Upvotes: 7

zag2art
zag2art

Reputation: 5172

Why don't you have just something like this:

router.post('/', function (req, res, next) {  //this is invoked first
    console.log(1);
    foo(req, res, next);
});

router.post('/', function (req, res, next) {  //this is invoked second
    console.log(2);
    bar(req, res, next);
});

function foo(req, res, next) { 
    console.log(3);
};

function bar(req, res, next) { 
    console.log(4);
};

router.post('/', function(req,res,next){  // this gets invoked third
    console.log(5);
});

Upvotes: 1

Related Questions