[英]How to list all paths in an Express application using Node.js, like "rake routes" command in Rails Application?
I've attempted to install several NPM packages into my express app that allow me to view my apps routes but I haven't had any success.我试图将几个 NPM 包安装到我的快速应用程序中,以允许我查看我的应用程序路线,但我没有取得任何成功。 I'm used to using rake routes
to display my routes in my rails applications but haven't found anything similar in express.我习惯于使用rake routes
在我的 rails 应用程序中显示我的路线,但在 express 中没有发现任何类似的东西。 What are the best ways to get a similar functionality in express?在 express 中获得类似功能的最佳方法是什么?
Have you looked into npm package pathfinder-ui ? 您是否看过npm package pathfinder-ui ? I'm not a Rails user, but I believe it's similar to rake routes
in the sense that it'll display all registered routes in an Express (4.x) app. 我不是Rails用户,但从某种意义上说,它与rake routes
类似,因为它将在Express(4.x)应用程序中显示所有已注册的路由。 It's also distinct in that: 它也与众不同:
It might be offering more than what you're looking for, but it should definitely display your Express routes in a dev-friendly manner and is pretty easy to configure. 它可能会提供比您想要的更多的东西,但是它绝对应该以开发者友好的方式显示Express路由,并且很容易配置。
I also just came across this package that might be worth a look as well: express-ls-routes . 我也刚看过这个包,可能值得一看: express-ls-routes 。
You can find another stack overflow question with same context of listing all paths/routes in an Express application in the given bellow post: 您可以在相同的上下文中找到另一个堆栈溢出问题,该问题在给定的波纹管文章中列出了Express应用程序中的所有路径/路由:
How to get all registered routes in Express? 如何在Express中获取所有已注册的路线?
For a reference you can also visit the following thread regarding "List All REST Endpoints built using Express or Restify" 作为参考,您还可以访问以下有关“列出使用Express或Restify构建的所有REST端点”的线程
Once if you successfully follow the last given link you will get the out put path/rout listed like bellow in the image: 一旦成功遵循了最后给定的链接,您将获得输出路径/路径,如图像中的波纹管所示:
You can run,你可以跑,
npm install express-route-list-cli
and then接着
npx route-list <relative app file path>
i created this code by reading above answers.我通过阅读上述答案创建了此代码。 this will not only return direct routes, but also routes created via router on specific path:这不仅会返回直接路由,还会返回通过路由器在特定路径上创建的路由:
const app = express()
let allRouters=[
["/api/v1/auth",AuthRouter],
["/api/v1/user",UserRouter]
]
app
.use(express.json())
.use(cookieParser())
allRouters.forEach(it=>app.use(it[0],it[1]))
app.listen(3000, () => console.log("server started at port 3000"))
app.get("/", (req, res) =>{
let routes = [
["/",app._router],
...allRouters
]
let routesJs = {}
routes.forEach(it=>{
let key = it[0]
let value = it[1].stack.filter(r => r.route).map(r => ({method: Object.keys(r.route.methods)[0].toUpperCase(), path: r.route.path}));
routesJs[key]=value
})
res.json(routesJs)
} )
the response for localhost:3000/
looked something like this: localhost:3000/
的响应如下所示:
{
"/": [
{ "method": "GET","path": "/" },
{ "method": "GET","path": "/mail"}
],
"/api/v1/auth": [
{"method": "POST","path": "/signup"},
{"method": "POST","path": "/login"},
{"method": "POST","path": "/securelogin"},
{"method": "PATCH","path": "/forgotpassword"},
{"method": "PATCH","path": "/resetpassword"}
],
"/api/v1/user": [
{"method": "PATCH","path": "/updateprofile"},
{"method": "GET","path": "/allusers"}
]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.