Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
136
rated 0 times [  141] [ 5]  / answers: 1 / hits: 137636  / 10 Years ago, tue, august 12, 2014, 12:00:00

Suppose I want to have REST endpoints which look roughly like this:



/user/
/user/user_id

/user/user_id/items/
/user/user_id/items/item_id


CRUD on each if makes sense. For example, /user POST creates a new user, GET fetches all users. /user/user_id GET fetches just that one user.



Items are user specific so I put them under user_id, which is a particular user.



Now to make Express routing modular I made a few router instances. There is a router for user, and a router for the item.



var userRouter = require('express').Router();
userRouter.route('/')
.get(function() {})
.post(function() {})
userRouter.route('/:user_id')
.get(function() {})

var itemRouter = require('express').Router();
itemRouter.route('/')
.get(function() {})
.post(function() {})
itemRouter.route('/:item_id')
.get(function() {})

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

// Now how to add the next router?
// app.use('/users/', itemRouter);


URL to item is descendents of the URL hierarchy of the user. Now how do I get URL with /users whatever to userRouter but the more specific route of /user/*user_id*/items/ to the itemRouter? And also, I would like user_id to be accessible to itemRouter as well, if possible.


More From » node.js

 Answers
7

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

[#69816] Saturday, August 9, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jimmieo

Total Points: 515
Total Questions: 102
Total Answers: 110

Location: Kazakhstan
Member since Mon, Sep 26, 2022
2 Years ago
;