Express.js:在req.param中如何使app.get('/ [:userId] i'..) [英] Express.js: how to make app.get('/[:userId]i'..) in req.param?
本文介绍了Express.js:在req.param中如何使app.get('/ [:userId] i'..)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要从url中读取 userId
像 http://mysite.com/39i
。这意味着 userId = 39
。
怎么办?谢谢。
示例:
app.get('/:userId_i',function(req,res){});
app.param('userId',function(req,res,next,id){});
解决方案
假设您有一个数字ID,后跟一个 i
并且您希望该ID只是数字。
应用程序。 get(/:id([0-9] +)i,function(req,res){...});
这将匹配一个数字,后跟一个 i
并将给你只是 req.params.id
中的数字。
示例:
curl -XGET localhost:8080 / 124i
在 req.params中给我以下内容
['id':124]
I m using nodejs 0.8.21 and express 3.1.0
I need to read userId
from url like http://mysite.com/39i
. It means userId=39
.
How to do? Thank you.
Sample:
app.get('/:userId_i', function(req, res) { });
app.param('userId', function(req, res, next, id){ });
解决方案
Assuming you have a numerical id followed by an i
and you want the id coming back as just the numerical.
app.get("/:id([0-9]+)i", function (req, res) {...});
This will match a numeric followed by an i
and will give you just the numeric in req.params.id
.
Example:
curl -XGET localhost:8080/124i
Gives me the following in req.params
[ 'id': 124 ]
这篇关于Express.js:在req.param中如何使app.get('/ [:userId] i'..)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文