node.js Express 中的多个可选路由参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41736413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multiple optional route parameters in Express?
提问by CLDev
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.
我的问题是,我如何让它们成为可选的?使用我定义的当前路由,除非所有三个参数都存在,否则将无法解析并落入默认路由。
回答by hjpotter92
The expressjs's guideto routing mentions:
该expressjs指南到路由提到:
Express uses
path-to-regexpfor matching the route paths; see the path-to-regexp documentation for all the possibilities in defining route paths. Express Route Testeris 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?
回答by R. Gulbrandsen
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 参数
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
定义要支持的 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
回答by micheal nayebare
This type of route is not likely to work because of the underscores in the parameters passed.
由于传递的参数中有下划线,这种类型的路由不太可能工作。
app.get('/products/:product_Id/buyers/:buyer_Id', function(req, res) {
getArticles(req.params.product_Id, req.params.buyer_Id);
}
So i suggest you use the following route system if the route is not working. There you will be able to send multiple parameters.
因此,如果路线不起作用,我建议您使用以下路线系统。在那里您将能够发送多个参数。
app.get('/products/:productId/buyers/:buyerId', function(req, res) {
getArticles(req.params.productId, req.params.buyerId);
}

