javascript 如何将参数传递给 express.js 路由器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30232258/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I pass a parameter to express.js Router?
提问by Sergey
Here's a modified example from Express.js's routing guide:
这是 Express.js路由指南中的修改示例:
var express = require('express');
var router = express.Router();
router.get('/', function(req, res) {
res.send('Birds home page');
});
router.get('/about', function(req, res) {
res.send('About birds');
});
...
app.use('/birds', router);
app.use('/fish', router);
This prints "About birds" when I visit both /birds/about
and /fish/about
.
当我访问/birds/about
和时,这会打印“关于鸟类” /fish/about
。
How do I pass a parameter or something to the router so, in the controller functions, it can tell those two different routes apart?
我如何将参数或其他东西传递给路由器,以便在控制器功能中,它可以区分这两个不同的路由?
For example, I'd like to see "Birds can fly" when visiting /birds/about
and "Fish can swim" when visiting /fish/about
.
例如,我想在访问时看到“鸟会飞”/birds/about
和“鱼会游泳” /fish/about
。
Ideally, I'd like to be able to pass some "configuration object" so the mini-app does not need to know about all possible routes it may be mounted at (in pseudocode):
理想情况下,我希望能够传递一些“配置对象”,因此迷你应用程序不需要知道它可能安装在的所有可能路由(以伪代码):
router.get('/about', function(req, res) {
res.send(magic_configuration.about_text);
});
....
magically_set_config(router, {about_text: "Bears eat fish"})
app.use('/bears', router);
回答by Sergey
Here's what I've come up with: I pass the "mini-app configuration" by assigning it to req
:
这是我想出的:我通过将“迷你应用程序配置”分配给req
:
app.use('/birds', function (req, res, next) {
req.animal_config = {
name: 'Bird',
says: 'chirp'
};
next();
}, animal_router);
app.use('/cats', function (req, res, next) {
req.animal_config = {
name: 'Cat',
says: 'meow'
}
next();
}, animal_router);
and then in my route I can access them:
然后在我的路线中我可以访问它们:
var express = require('express');
var router = express.Router();
...
router.get('/about', function(req, res) {
var animal = req.animal_config;
res.send(animal.name + ' says ' + animal.says);
});
This approach allows to easily mount the "mini-app" at another location providing different configuration, without modifying the code of the app:
这种方法允许在提供不同配置的另一个位置轻松安装“迷你应用程序”,而无需修改应用程序的代码:
app.use('/bears', function (req, res, next) {
req.animal_config = {
name: 'Bear',
says: 'rawr'
};
next();
}, animal_router);
回答by vanadium23
So, if you want to serve changes by url, then you can inject params like this:
因此,如果您想通过 url 提供更改,那么您可以像这样注入参数:
router.get('/:animal/about', function(req, res) {
// here we have bird or fish in req.params.animal
if(req.params.animal == 'bird') {
res.send('Birds can fly');
} else if(req.params.animal == 'fish') {
res.send('Fish can swim');
} else {
res.send('Unknown animal');
}
});
app.use('/', router);
回答by Nat
You can use req.baseUrlto figure that out.
您可以使用req.baseUrl来解决这个问题。
回答by Anton Savchenko
You can add route params like so:
您可以像这样添加路由参数:
router.get('/about/:param1/:param2', function(req, res) {
//then you can call this handler through /about/1/sometext get these params from request object:
console.log(req.params.param1, req.params.param2); // 1, 'sometext'
res.send('About birds');
});
Or you can send parameters through query params:
或者您可以通过查询参数发送参数:
router.get('/about', function(req, res) {
//then you can call this handler through /about?param1=1¶m2=sometext get these params from request object as well:
console.log(req.query.param1, req.query.param2); // 1, 'sometext'
res.send('About birds');
});