Stephan K.
Stephan K.

Reputation: 15752

Express.js / Node.js: Order of Routes important and where to put next()?

I have my routes and path set up like this:

var path1        = express.Router()
  , path2        = express.Router()
  // .. more modules, e.g. express-jwt ..

module.exports = function(app, passport) {

  var path1Func = function(req, res) {

  return MODEL.findById(req.params.id, function(err, model) {

      if(!model) {
        res.statusCode = 404;
        return res.send({ status: '404 Not found' });
      }

      if(!err) {
        return res.send({ status: 'OK', model:model });
      } else {
        res.statusCode = 500;
        return res.send({ status: '500 Server error' });
      }
    });
  };

  // .. other functions which essentially look like above path1Func ..

  path1.get('/subPath',            path1Func             );
  path2.get('/otherSubPath',            path2Func             );
  path2.post('/entirelyOtherSubPath',           otherPath2Func        );

  app.use('/path1', path1);
  app.use('/path2', path2);

  app.use('/grid', expressJwt({secret: secret}));  // Protect Paths
}

Now, Depending on ordering of my HTTP requests, POST, GET, etc, they get executed or returning a 404 not found, meaning that express does not resolve requests to some requests, depending of ordering of path1.get, path2.get, path2.post.

express1Order

express2Order

Above Images depict the 404 I am seeing, depending on the order of the route declarations in my routes.js file. When I reshuffle my order, the 404 is gone and I am served my wanted output

This is why I want to employ next() to help me with that - keeping in mind I am using plenty of app.use to complicate matters. I did not manage to do this on my own so far. I am still not clear what next() exactly is and how to use it in above example to make my router work in every route. I did not put my other routes here to save space, but my API is already quite big and will grow even more.

What would be an approach here to make my app serve content, no matter what order my POST and GET requests are ordered?

Upvotes: 1

Views: 1721

Answers (2)

Jordonias
Jordonias

Reputation: 5848

I believe you are using your Routers wrong

path1.get('/path1',            path1Func             );
path2.get('/path2',            path2Func             );
path2.post('/path2',           otherPath2Func        );

app.use('/path1', path1);
app.use('/path2', path2);

This will use the routes /path1/path1 and /path2/path2 When what you want is:

path1.get('/',            path1Func             );
path2.get('/',            path2Func             );
path2.post('/',           otherPath2Func        );

app.use('/path1', path1);
app.use('/path2', path2);

Although I'm not sure how expressJwt is.

I don't see anywhere you would need to use next since all of the route path's look unique. You may need to provide more information if this isn't your issue.

Upvotes: 1

mscdex
mscdex

Reputation: 106746

The reason that particular request you've shown gets a 404 is because you don't have a route pattern that matches.

You're requesting /grid/dates/:val but you only have a /grid route. Even if you change /grid to /grid/dates/:val, you still aren't responding to the request because all expressJwt() does is validate the request and then passes execution to the next handler. So with that in mind, you might try something like:

app.use('/grid/dates/:val',
        expressJwt({secret: secret}),
        function(req, res) {
  res.send(200, 'hello world!');
});

Upvotes: 1

Related Questions