使用 Express.js 嵌套路由器休息

编程入门 行业动态 更新时间:2024-10-26 08:31:23
本文介绍了使用 Express.js 嵌套路由器休息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

假设我想要大致如下所示的 REST 端点:

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

每个 if 的 CRUD 都有意义.例如,/user POST 创建一个新用户,GET 获取所有用户./user/user_id GET 只获取那个用户.

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.

项目是用户特定的,所以我把它们放在 user_id 下,这是一个特定的用户.

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

现在为了使 Express 路由模块化,我制作了一些路由器实例.用户有路由器,物品有路由器.

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

item 的 URL 是 user 的 URL 层次结构的后代.现在我如何使用 /users 获取 URL 到 userRouter 但是/user/*user_id*/items/ 到 itemRouter 的更具体的路由?而且,如果可能,我希望 itemRouter 也可以访问 user_id.

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.

推荐答案

您可以通过将路由器附加为 中间件来嵌套路由器 在其他路由器上,带或不带 params.

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

如果您想从父路由器访问 params,您必须将 {mergeParams: true} 传递给子路由器.

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

mergeParams 是在 Express 4.5.0 中引入的>(2014 年 7 月 5 日)

mergeParams was introduced in Express 4.5.0 (Jul 5 2014)

在这个例子中,itemRouter 被附加到 /:userId/items 路由上的 userRouter

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 userGET/user/5 -> hello user 5GET/user/5/items -> hello items from user 5GET/user/5/items/6 -> hello item 6 from user 5

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

更多推荐

使用 Express.js 嵌套路由器休息

本文发布于:2023-11-25 17:15:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1630634.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   路由器   Express   js

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!