0

We are creating a NodeJS application using express framework. While working with router params we encountered a unexpected behavior. API is being tested in postman as we don't have the frontend ready. There are two API defined as below

var router = express.Router();
router.get('/tif/:test2/:one', (req, res) => {
  res.send("Test two");
});

router.get('/tif/test1/:one', (req, res) => {
  res.send("Test one");
});

module.exports = router;

From postman we give an request 'http://localhost:3000/api/tif/test1/1', response received is 'Test two' wherein it should have responded 'Test one'. By changing the order of router I am able to get expected response 'Test one'. We are unable to reason out this behavior.

James Shaji
  • 316
  • 5
  • 17
  • 1
    :test2 inside '/tif/:test2/:one' means its a parameter, so you can accept anything in there, so when you are doing request 'http://localhost:3000/api/tif/test1/1', test1 is passed as a parameter value to :test2. So in short anything, after /tif/ will be accepted by :test2. That's why you are getting this behavior. – Rahul Raval Oct 11 '18 at 13:31
  • Best practice would be to use '/tif/test1/:one' before '/tif/:test2/:one'. – Rahul Raval Oct 11 '18 at 13:33

1 Answers1

1

Routes are checked for a match to the incoming URL in the order they are registered on the router and in the order the routers are registered on the app. In your case, the first route finds a match to the route and handles the request and routing is not continued to any other route handlers.

First off, I assume this router is assigned to /api so it sees all routes that start with /api. You don't show that code, but it appears to be what you must be doing.

So, because '/tif/:test2/:one' is more general than '/tif/test1/:one' and that first one will match your URL, /api/tif/test1/1, the second route never gets a chance.

By changing the order of router I am able to get expected response 'Test one'. We are unable to reason out this behavior.

When you put '/tif/test1/:one' first, it gets first chance at /api/tif/test1/1 and will match it so you will get the desired behavior.

A general rule is this: "If you have multiple routes that could match a given URL, put the less general routes first before the more general routes so they get a chance to match a URL that fits them". If you don't do that, then the less general routes will never get a chance to match because the more general routes will match first and "handle" the request before the others every got a chance to see them.

jfriend00
  • 683,504
  • 96
  • 985
  • 979