Express.js: how to make app.get('/[:userId]i'..) in req.param?

扶醉桌前 提交于 2019-11-30 17:43:35

问题


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){ });

回答1:


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 ]


来源:https://stackoverflow.com/questions/15482959/express-js-how-to-make-app-get-useridi-in-req-param

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!