Express.js - Filter a mongodb id in the URL

℡╲_俬逩灬. 提交于 2019-12-01 21:29:35

You're almost there, just don't add the ^ and $ anchors. And the uppercase A-F range isn't even necessary since Express seems to match case-insensitive:

app.post('/:mongoId([0-9a-f]{24})', function(req, res){
  var id = req.param('mongoId');
  ...
});

According to the Express API documentation, yes, you can use a regular expression as a path:

Regular expressions may also be used, and can be useful if you have very specific restraints.

app.get(/^\/commits\/(\w+)(?:\.\.(\w+))?$/, function(req, res){
  var from = req.params[0];
  var to = req.params[1] || 'HEAD';
  res.send('commit range ' + from + '..' + to);
}); 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!