在我基于Express和Node的项目中,当我访问http://localhost:3000/users/lbs
时,req.params.name"应该等于' lbs'但它是未定义的。
//app.js
app.use("/users/:name",username);
//username.js
var express = require('express');
var router = express.Router();
router.get('/', function(req, res) {
res.send('user: ' + req.params.name);
});
module.exports = router;
答案 0 :(得分:5)
我认为您应该将其更改为:
//app.js
app.use("/users",username);
//username.js
var express = require('express');
var router = express.Router();
router.get('/:name', function(req, res) {
res.send('user: ' + req.params.name);
});
module.exports = router;
答案 1 :(得分:1)
试试这个解决方案:
//app.js
app.use("/users",username);
//username.js
var express = require('express');
var router = express.Router();
router.get('/:name', function(req, res) {
res.send('user: ' + req.param('name'));
});
module.exports = router;