如何将参数传递给express.js路由器?

时间:2021-10-04 07:25:13

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.

当我访问/鸟/约和/鱼/约时,这打印“关于鸟”。

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.

例如,我想在访问/观鸟/约会时看到“鸟可以飞”,在参观/钓鱼/约时,我想看“鱼可以游泳”。

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);

4 个解决方案

#1


7  

Here's what I've come up with: I pass the "mini-app configuration" by assigning it to req:

这就是我想出的:我将“mini-app配置”分配给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);

#2


4  

So, if you want to serve changes by url, then you can inject params like this:

所以,如果你想通过url提供更改,那么你可以像这样注入params:

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);

#3


1  

You can use req.baseUrl to figure that out.

您可以使用req.baseUrl来计算出来。

#4


0  

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&param2=sometext get these params from request object as well:
  console.log(req.query.param1, req.query.param2); // 1, 'sometext'
  res.send('About birds');
});

#1


7  

Here's what I've come up with: I pass the "mini-app configuration" by assigning it to req:

这就是我想出的:我将“mini-app配置”分配给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);

#2


4  

So, if you want to serve changes by url, then you can inject params like this:

所以,如果你想通过url提供更改,那么你可以像这样注入params:

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);

#3


1  

You can use req.baseUrl to figure that out.

您可以使用req.baseUrl来计算出来。

#4


0  

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&param2=sometext get these params from request object as well:
  console.log(req.query.param1, req.query.param2); // 1, 'sometext'
  res.send('About birds');
});