Rest with Express.Js Nested Router

Rest with Express.js nested router

You can nest routers by attaching them as middleware on an other router, with or without params.

You must pass {mergeParams: true} to the child router if you want to access the params from the parent router.

mergeParams was introduced in Express 4.5.0 (Jul 5 2014)

In this example the itemRouter gets attached to the userRouter on the /:userId/items route

This will result in following possible routes:

GET /user -> hello user
GET /user/5 -> hello user 5
GET /user/5/items -> hello items from user 5
GET /user/5/items/6 -> hello item 6 from user 5

var express = require('express');
var app = express();

var userRouter = express.Router();
// you need to set mergeParams: true on the router,
// if you want to access params from the parent router
var itemRouter = express.Router({mergeParams: true});

// you can nest routers by attaching them as middleware:
userRouter.use('/:userId/items', itemRouter);

userRouter.route('/')
.get(function (req, res) {
res.status(200)
.send('hello users');
});

userRouter.route('/:userId')
.get(function (req, res) {
res.status(200)
.send('hello user ' + req.params.userId);
});

itemRouter.route('/')
.get(function (req, res) {
res.status(200)
.send('hello items from user ' + req.params.userId);
});

itemRouter.route('/:itemId')
.get(function (req, res) {
res.status(200)
.send('hello item ' + req.params.itemId + ' from user ' + req.params.userId);
});

app.use('/user', userRouter);

app.listen(3003);

Node.js Express nested resources

You may try this solution:

//declare a function that will pass primary router's params to the request
var passPrimaryParams = function(req, res, next) {
req.primaryParams = req.params;
next();
}

/// fileA.js
app.use('/dashboards/:dashboard_id/teams', passPrimaryParams);
app.use('/dashboards/:dashboard_id/teams', teams);

///teams.js
router.get('/', function(req, res, next) {
var dashboardId = req.primaryParams['dashboard_id']; //should work now
//here you may also use req.params -- current router's params
}

ExpressJS nested route with param validation

This looks like a misunderstanding of how nested routers work. In your example you seem to be looking to share a param across roomroute and messageroute, however, both of those routers have no relation to eachother.

Routers become nested when they're supplied as middleware to another router - you have an example of this already....app is a router in itself and you nest both roomroute and messageroute into it. So based on your current setup, if you want to share param('room') across both these routes you will need to configure it at app level i.e.

main.js

const roomRoute = require('roomroute.js');
const messageRoute = require('messageroute.js');

app.param('room', function (req,res,next,id) {
// Just for demo
var room = {
id: id,
title: 'Testroom'
};
req.room = room;
next();
});
app.use('/room', roomRoute);
app.use('/room/:room/messages', messageRoute);

roomroute.js

const router = express.Router({ mergeParams: true });
router.get('/:room', ...);

messageroute.js

const router = express.Router({ mergeParams: true });
router.get('/', ...);

Express Nested Router not calling sub functions

I fixed it, instead of

router.use("/:collection/", (req) => {    return require(`./${req.params.collection}`);});

Express Router with nested params doesn't work

The express documentation does not mention the possiblity of nested parameters.

But

this.router.get(
`${this.path}/watch/:cameraname([a-zA-Z\_]+).:suffix`,
...)

works and gives you the suffix (that is, the rest of the filename) as req.params.suffix.

Does that cover your requirement?



Related Topics



Leave a reply



Submit