Express.js:如何在 req.param 中制作 app.get('/[:userId]i'..) ? [英] Express.js: how to make app.get('/[:userId]i'..) in req.param?

查看:18
本文介绍了Express.js:如何在 req.param 中制作 app.get('/[:userId]i'..) ?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 nodejs 0.8.21 并表达 3.1.0

I m using nodejs 0.8.21 and express 3.1.0

我需要从像 http://mysite.com/39i 这样的 url 读取 userId.这意味着 userId=39.怎么做?谢谢你.示例:

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

推荐答案

假设您有一个数字 id 后跟一个 i 并且您希望 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) {...});

这将匹配一个数字,后跟一个 i,并且只会给你 req.params.id 中的数字.

This will match a numeric followed by an i and will give you just the numeric in req.params.id.

示例:

curl -XGET localhost:8080/124i

req.params

[ 'id': 124 ]

这篇关于Express.js:如何在 req.param 中制作 app.get('/[:userId]i'..) ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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