Express.js: how to make app.get('/[:userId]i&#

2019-02-14 05:38发布

问题:

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 ]