3

I've read up other questions on people's routes mismatching and then ordering the routes solving the problem. I've got this problem where my URL route is being treated as a parameter and then express mismatches and leads to the wrong route. e.g. here are the two routes:

app.get('/byASIN/LowPrice/:asin/:price',function(req,res){});

and

app.get('/byASIN/:asin/:price', function(req, res) {});

Now all works fine but as soon as I take any param out of the first route it matches the route given below which is not what I want. If I hit /byASIN/LowPrice/:asin/:price everything works fine but as soon as I hit /byASIN/LowPrice/:asin it matches byASIN/:asin/:price and hence calls the wrong function and crashes my server. I would like to have them match explicitly and if /byASIN/LowPrice/:asin is called, respond with some warning e.g. you're calling with one less argument. What am I missing here?

user3677331
  • 698
  • 2
  • 7
  • 22

3 Answers3

3

By default express Url parameters are not optinial, this is why

app.get('/byASIN/LowPrice/:asin/:price',function(req,res){});

does not match /byASIN/LowPrice/:asin, because the second parameter is missing.

However you can make a parameter optional by adding a ? to it:

app.get('/byASIN/LowPrice/:asin/:price?',function(req,res){});

this should solve your problem.

Nick D
  • 1,483
  • 1
  • 13
  • 22
  • I don't want to make it optional, I just want the router to strictly match. IMO `/byASIN/LowPrice/` is worlds different from `/byASIN/:asin/` and I would like the match to be strict and if there's a param missing issue a warning that the param is missing instead of having it move to the next totally different route – user3677331 Apr 26 '16 at 16:01
0

Try to define a route for /byASIN/LowPrice/:asin/:price to handle, then use a wildcard to handle everything else.

app.get('/byASIN/LowPrice/:asin/:price',function(req,res){});

app.get('*',function(req,res){});
BrTkCa
  • 4,703
  • 3
  • 24
  • 45
  • Sorry I didn't understand this. just like `byASIN/:asin/:price` I have many other routes which get matched. I think what it's doing is, first it checks if the top one matches, even though `/LowPrice/:asin` matches but the number of params does not match so it moves to `byASIN/:asin/:price` because it takes `/LowPrice` as a param too and hence making a total of 3 params which is wrong – user3677331 Apr 26 '16 at 17:26
0

Express matches the route by the order you insert them. If you have the loosely routes defined first, then express will use that one as the match first. An extreme example would be

app.get('*', function(req, res) {});

If this was defined as the first route, then no other route will be called (if without calling next()).

If you want express to always use the strict one first, then you will need to change the order of your routes by having the strict ones defined before the loosely ones.

It'd be nice if express support priority in the route, which could be a good solution for your problem, but until then, unfortunately, this can be fixed by ordering only :(

Anthony C
  • 2,117
  • 2
  • 15
  • 26