我使用express为node.js获取冒号

这是我的路线:

router.route('/search:word').get(function(req, res) { var re = new RegExp(req.params.word, 'i'); console.log(req.params.word); Place.find().or([{ 'title': { $regex: re }}, { 'category': { $regex: re }}]).sort('title').exec(function(err, places) { res.json(JSON.stringify(places)); }); }); 

当我使用请求/places/search:test控制台显示:testing,而不是只是“testing”。 有什么想法发生了什么?

你真的要使用router.route('/search:word')而不是router.route('/search/:word')吗? 使用冒号似乎有点奇怪。

如果你使用router.route('/search/:word') ,并且你的请求是

/places/search/test

那么你得到req.params.word="test"

这里发生的事情是,它正在打破/search[word]的path。 所以当你请求/search:test ,[word]匹配/search之后的部分,在这种情况下会是:test

你可能需要的是类似于: /search::word