简体   繁体   English

前缀 express.js 路由

[英]Prefix express.js Routes

Is there a way to prefix a subset of routes in express.js?有没有办法在 express.js 中为路由子集添加前缀? I know that you can add a mount point when calling the app.router() but this affects all routes.我知道您可以在调用 app.router() 时添加挂载点,但这会影响所有路由。

I am looking for a way to add /api/v1/ to a bunch of routes instead of applying this to every single route definition.我正在寻找一种将/api/v1/添加到一堆路由的方法,而不是将其应用于每个路由定义。

express-namespace做你想要的。

Another possible solution: Check this library, it allows you to softcode your routes and add hierarchies to them.另一种可能的解决方案:检查这个库,它允许您对路由进行软编码并向它们添加层次结构。 You can add a simple route with no handler that responds to /api/v1 and have all other routes be a child of it.您可以添加一个没有处理程序的简单路由来响应 /api/v1 并让所有其他路由成为它的子路由。

https://github.com/hrajchert/express-shared-routes https://github.com/hrajchert/express-shared-routes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM