我在app.js里写的: app.use('/user/:username’,user); 在路由user中写的: router.get('/’, function(req, res, next) { res.render(‘user: ’ + req.params[0]); });
这样写运行后为什么得不到值? 新手求指导,拜谢了!
3 回复
这类问题让我挺无语的。 因为一看就知道是没有看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"