Express中的多个可选路由参数?

时间:2022-06-08 10:59:22

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.

我使用Express来处理格式为/ articles /:year /:month /:day的路线,其中年,月和日是可选的。

  • 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.

我的问题是,如何让它们成为可选项?根据我定义的当前路线,除非存在所有三个参数,否则它将无法解析并将落入默认路线。

2 个解决方案

#1


19  

The expressjs's guide to routing mentions:

expressjs的路由指南提到:

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.

Express使用path-to-regexp来匹配路径路径;有关定义路径路径的所有可能性,请参阅path-to-regexp文档。 Express Route Tester是一个用于测试基本Express路由的便捷工具,但它不支持模式匹配。

Basically, you can use the ? character to make the parameter optional.

基本上,你可以使用?使参数可选的字符。

/articles/:year?/:month?/:day?

#2


8  

Edited for own purpose of having the 3 different options in one answer. Credit to @hjpotter92 for his regex answer.

编辑的目的是在一个答案中有3个不同的选项。感谢@ hjpotter92的正则表达式答案。

With URL Params

使用URL Params

With regex

用正则表达式

app.get('/articles/:year?/:month?/:day?', function(req, res) {
  var year = req.query.year; //either a value or undefined
  var month = req.query.month;
  var day = req.query.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

定义要支持的3个路径并重用相同的功能

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:

此端点的URL将如下所示:

http://localhost/articles?year=2016&month=1&day=19

#1


19  

The expressjs's guide to routing mentions:

expressjs的路由指南提到:

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.

Express使用path-to-regexp来匹配路径路径;有关定义路径路径的所有可能性,请参阅path-to-regexp文档。 Express Route Tester是一个用于测试基本Express路由的便捷工具,但它不支持模式匹配。

Basically, you can use the ? character to make the parameter optional.

基本上,你可以使用?使参数可选的字符。

/articles/:year?/:month?/:day?

#2


8  

Edited for own purpose of having the 3 different options in one answer. Credit to @hjpotter92 for his regex answer.

编辑的目的是在一个答案中有3个不同的选项。感谢@ hjpotter92的正则表达式答案。

With URL Params

使用URL Params

With regex

用正则表达式

app.get('/articles/:year?/:month?/:day?', function(req, res) {
  var year = req.query.year; //either a value or undefined
  var month = req.query.month;
  var day = req.query.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

定义要支持的3个路径并重用相同的功能

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:

此端点的URL将如下所示:

http://localhost/articles?year=2016&month=1&day=19