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);
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.
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);
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With