【发布时间】:2015-09-07 13:02:16
【问题描述】:
我正在使用 restify 2.8.4。
了解不支持带有正则表达式的命名参数 Mixing regex and :params in route #247
而不是将两个逻辑拼凑到一个块中。
server.get ('/user/:id', function (req, res, next) {
var id = req.params.id;
// check with isNaN()
// if string do this
// if number do that
}
我更喜欢下面的代码结构:
//hit this route when named param is a number
server.get (/user\/:id(\\d+)/, function (req, res, next) {
var id = req.params.id;
// do stuff with id
}
//hit this route when named param is a string
server.get (/user\/:name[A-Za-z]+/, function (req, res, next) {
var name = req.params.name;
// do stuff with name
}
有没有办法可以将它们分成两个独立的关注点?
【问题讨论】:
标签: restify