CNode

为什么通过req.params[0]得不到数据

问答
Ppoormonkey发布于11 年前最后回复11 年前3 回复4646 浏览0 收藏

我在app.js里写的: app.use('/user/:username',user); 在路由user中写的: router.get('/', function(req, res, next) { res.render(‘user: ’ + req.params[0]); });

这样写运行后为什么得不到值? 新手求指导,拜谢了!

查看回复

回复 (3)

J
jinceon#1·11 年前

这类问题让我挺无语的。 因为一看就知道是没有看api文档的。

req.params An object containing properties mapped to the named route “parameters”. For example, if you have the route /user/:name, then the “name” property is available as req.params.name. This object defaults to {}. req.params默认是{},不是数组。

/user/:name

// GET /user/tj
req.params.name
// => "tj"

http://expressjs.com/4x/api.html#req.params

D
DevinXian#2·11 年前

router是express.Router()?

D
DevinXian#3·11 年前

req.params.xxx,如果有特殊字符req.params.['xxx']。就是一数组

参与回复
登录后即可参与回复。登录