How to pass parameter to express.js Router?

Here's a modified example from Express.js routing guide :

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

      

It prints "About Birds" when I visit both /birds/about

and /fish/about

.

How can I pass a parameter or something to the router so that in controller functions it can separate these two different routes?

For example, I would like to see "Birds can fly" when visiting /birds/about

and "Fish can swim" when visiting /fish/about

.

Ideally, I would like to be able to pass in some "config object", so the widget does not need to know about all the possible routes in which it can be set (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);

      

+3


source to share


4 answers


Here's what I came up with: passing in the "widget config" assigning it 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 on 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 makes it easy to mount a "widget" elsewhere, providing different configurations without changing the application code:

app.use('/bears', function (req, res, next) {
    req.animal_config = {
        name: 'Bear',
        says: 'rawr'
    };
    next();
}, animal_router);

      

+5


source


So, if you want to serve changes by url, you can enter parameters like this:



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


source


You can use req.baseUrl to figure it out.

+1


source


You can add route parameters like this:

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 via request parameters:

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

      

0


source







All Articles