Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass a parameter to express.js Router?

Tags:

Here's a modified example from Express.js's 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); 

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); 
like image 828
Sergey Avatar asked May 14 '15 08:05

Sergey


People also ask

What are Express route parameters?

In Express, route parameters are essentially variables derived from named sections of the URL. Express captures the value in the named section and stores it in the req. params property. You can define multiple route parameters in a URL.


2 Answers

Here's what I've come up with: I pass the "mini-app configuration" by assigning it to 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); 
like image 197
Sergey Avatar answered Oct 10 '22 16:10

Sergey


You're basically talking about injecting configuration to a router.

I have faced with similar problem and figured out that in theory you can export not a router itself, but rather function that accepts configuration and returns created and configured router.

So in your case calling code will look like:

var animal_router = require('./animal_router')  app.use('/birds', animal_router({     name: 'Bird',     says: 'chirp' }));  app.use('/cats', animal_router({     name: 'Cat',     says: 'meow' })); 

While ./animal_router.js might look following:

var express = require('express');  // Create wrapper function that will adjust router based on provided configuration var wrapper = function (animal_config) {     var router = express.Router();          router.get('/about', function(req, res) {         var animal = animal_config;         res.send(animal.name + ' says ' + animal.says);     });      return router; }  module.exports = wrapper; 
like image 38
shytikov Avatar answered Oct 10 '22 17:10

shytikov