The following:
以下几点:
app.get('/foo/start/:start/end/:end', blah.someFunc);
matches
匹配
/foo/start/1/end/4
but I want it to also match an optional parameter
但我希望它也匹配一个可选参数
/foo/start/1/end/4/optional/7
I've tried this:
我已经试过这个:
app.get('/foo/start/:start/end/:end(/optional/:value)?', blah.someFunc);
but it doesn't match either of the above two examples. I think it's because I'm trying to give it a RegExp
when it's expecting something else?
但这两个例子都不匹配。我想是因为我试图给它一个RegExp当它期望别的东西时?
Thanks.
谢谢。
4 个解决方案
#1
20
Why don't you add another rule before the one you have, like this
你为什么不在已有的规则之前再加一条,像这样
app.get('/foo/start/:start/end/:end/optional/:value', blah.someFunc);
app.get('/foo/start/:start/end/:end', blah.someFunc);
It will be used before the one without the optional value.
它将在没有可选值的值之前使用。
If you want to use just one line try this:
如果你只想用一行的话,试试这个:
app.get('/foo/start/:start/end/:end/optional?', blah.someFunc)
see the docs for an example.
请参阅文档中的示例。
#2
15
If you are using Express 4.x Then I think its better to use array format for route. For example I have route /service
which gives all service list and same route when used with id /service/id/:id
gives single service with id in the param.
如果你使用的是Express 4。然后我认为最好使用数组格式来进行路由。例如,在使用id/ service/id/ id时,我有提供所有服务列表和相同路由的路由/服务:id在param中提供id。
app.get(['/service', '/service/id/:id'], function(req, res) {});
#3
7
In this example, if the url is /hello or /hello/world it works. The ? makes the parameter become optional (express 4).
在本例中,如果url是/hello或/hello/world,那么它就可以工作。的吗?使参数成为可选的(express 4)。
// app.js
var index = require('/routes/index');
app.use('/hello', index);
// routes/index.js
router.get('hello/:name?', function(req, res){
var name = req.params.name;
var data = {
name: name
};
res.json(data);
});
#4
5
You can also use regular expressions in routes. Perhaps something like:
您还可以在路由中使用正则表达式。也许类似:
app.get(/^\/foo\/start\/:start\/end\/:end(\/optional\/:value)?/, function (req, res, next) {
#1
20
Why don't you add another rule before the one you have, like this
你为什么不在已有的规则之前再加一条,像这样
app.get('/foo/start/:start/end/:end/optional/:value', blah.someFunc);
app.get('/foo/start/:start/end/:end', blah.someFunc);
It will be used before the one without the optional value.
它将在没有可选值的值之前使用。
If you want to use just one line try this:
如果你只想用一行的话,试试这个:
app.get('/foo/start/:start/end/:end/optional?', blah.someFunc)
see the docs for an example.
请参阅文档中的示例。
#2
15
If you are using Express 4.x Then I think its better to use array format for route. For example I have route /service
which gives all service list and same route when used with id /service/id/:id
gives single service with id in the param.
如果你使用的是Express 4。然后我认为最好使用数组格式来进行路由。例如,在使用id/ service/id/ id时,我有提供所有服务列表和相同路由的路由/服务:id在param中提供id。
app.get(['/service', '/service/id/:id'], function(req, res) {});
#3
7
In this example, if the url is /hello or /hello/world it works. The ? makes the parameter become optional (express 4).
在本例中,如果url是/hello或/hello/world,那么它就可以工作。的吗?使参数成为可选的(express 4)。
// app.js
var index = require('/routes/index');
app.use('/hello', index);
// routes/index.js
router.get('hello/:name?', function(req, res){
var name = req.params.name;
var data = {
name: name
};
res.json(data);
});
#4
5
You can also use regular expressions in routes. Perhaps something like:
您还可以在路由中使用正则表达式。也许类似:
app.get(/^\/foo\/start\/:start\/end\/:end(\/optional\/:value)?/, function (req, res, next) {