Express.js - 过滤URL中的mongodb id [英] Express.js - Filter a mongodb id in the URL

查看:155
本文介绍了Express.js - 过滤URL中的mongodb id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题的灵感来源于这篇文章但是在我的情况下,我需要过滤MongoId。

This question inspired by this post but in my case I need to filter MongoId. Is it possible to make filtering easily that the below because I need use it in each route?

app.post('/:mongoId(^[0-9a-fA-F]{24}$)', function(req, res){
   // Send query based on mongoId
}


推荐答案

你几乎在那里,只是不要添加 ^ $ 锚点,而大写的 AF 范围甚至不需要,因为Express似乎匹配不区分大小写:

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');
  ...
});

这篇关于Express.js - 过滤URL中的mongodb id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆