简体   繁体   English

在Express中将参数路由空

[英]Route parametrs empty in Express

I try print router parametrs but req.params is empty, don't know what I'm doing wrong. 我尝试打印路由器参数,但req.params是空的,不知道我做错了什么。

In app.js: 在app.js中:

...
var shareFile=require('./controllers/file/share');
...
app.use('/share/:id', shareFile);
...

And share.js controller: 和share.js控制器:

var express = require('express');
var router = express.Router()

router.get('/', function (req, res, next) {
    res.send(req.params)
});

module.exports = router;

localhost:3000/share/123 gives empty json page. localhost:3000/share/123给出空的json页面。 Changing res.send to console.log gives {} . res.send更改为console.log会给出{}

Thanks 谢谢

req.params will produce {id: 'abc'} when the routing is defined as /share/:id and you made the call to http://yourserver/share/abc 当路由定义为/share/:id并且您调用http://yourserver/share/abc时,req.params将生成{id: 'abc'}

router.get('/', function (req, res, next) {
    res.send(req.params)
});

Above you should expect that req.params will be empty here because your are not expecting them. 在上面你应该期望req.params在这里是空的,因为你没有期待它们。 But you should see them here: 但你应该在这里看到它们:

router.get('/share/:id', function (req, res, next) {
    res.send(req.params)
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM