Multiple optional route parameters in Express?
Solution 1
The expressjs's guide to routing mentions:
Express uses
path-to-regexp
for matching the route paths; see the path-to-regexp documentation for all the possibilities in defining route paths. Express Route Tester is a handy tool for testing basic Express routes, although it does not support pattern matching.
Basically, you can use the ?
character to make the parameter optional.
/articles/:year?/:month?/:day?
Solution 2
Edited for own purpose of having the 3 different options in one answer. Credit to @hjpotter92 for his regex answer.
With URL Params
With regex
app.get('/articles/:year?/:month?/:day?', function(req, res) {
var year = req.params.year; //either a value or undefined
var month = req.params.month;
var day = req.params.day;
}
Without regex
var getArticles = function(year, month, day) { ... }
app.get('/articles/:year', function(req, res) {
getArticles(req.params.year);
}
app.get('/articles/:year/:month', function(req, res) {
getArticles(req.params.year, req.params.month);
}
app.get('/articles/:year/:month/:day', function(req, res) {
getArticles(req.params.year, req.params.month, req.params.day);
}
Define the 3 paths you want to support and reuse the same function
With Query Params
app.get('/articles', function(req, res) {
var year = req.query.year; //either a value or undefined
var month = req.query.month;
var day = req.query.day;
}
The url for this endpoint will look like this:
http://localhost/articles?year=2016&month=1&day=19
CLDev
Updated on July 05, 2022Comments
-
CLDev almost 2 years
I am using Express to handle a route which is in the format of
/articles/:year/:month/:day
, where year, month and day are optional.- If none of the three params is given, all articles will be returned;
- If year is given, articles of that year will be returned;
- If year and month are given, articles of that year and month will be returned;
- If all three params are given, articles of that year, month and day will be returned.
My question is, how do I make them optional? With the current route I've defined, unless all three parameters are present, it will not be able to be resolved and will fall into the default route.
-
CLDev over 7 yearsThanks a lot for your help. I think they tackle my use case perfectly (especially the multi-route proposal), just that hjpotter92 has suggested an even simpler solution. Thanks again for your help anyways!
-
The Fool about 4 yearsSince when can't you have an underscore in your variables?
-
tbhaxor over 3 yearsAlso Open API Standards recommends using query parameter for optional parameters.
-
Andres Espinosa over 3 yearsIt seems that it doesn't work. If you try that route with the route tester,
/articles//1/2
for example with not match. If I use only one optional parameter it works -
hjpotter92 over 3 years@AndresEspinosa did you go through
path-to-regexp
? You can use*
for any nested paths -
Andres Espinosa over 3 yearsYes, I did. I think
*
is a bad practice because it would match many unexpected paths. Anyway, what you're saying is to use something like/articles/*
right? I would have to then parse the rest of the path by myself then? -
hjpotter92 over 3 years@AndresEspinosa it depends on how deep the paths you're expecting.