简体   繁体   English

如何使用 Node.js 列出 Express 应用程序中的所有路径,例如 Rails 应用程序中的“rake routes”命令?

[英]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's a GUI that lets you view all your routes as either 1.) a table that can be filtered/searched, or 2.) an expandable tree diagram 它是一个GUI,可让您以1.)可以过滤/搜索的表格或2.)可扩展树形图的形式查看所有路由
  • lets you test your routes with a request builder - an alternative to curl (kind of like using POSTMAN if you've ever worked with it). 可让您使用请求构建器测试路由-一种卷曲的替代方法(有点像使用POSTMAN,如果您曾经使用过POSTMAN)。

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.

相关问题 如何使用 Node.js 列出 Express 应用程序中的所有端点,例如“Loopback explorer” - How to list all endpoints in an Express application using Node.js, like “Loopback explorer” 在hogan-express node.js express应用程序中,如何在特定路由中使用与全局模板不同的模板? - In a hogan-express node.js express application, How can I use different template than global template in particular routes? Node.js/Express:通过单页应用程序传递路由/让除根之外的其他路由处理 - Node.js/Express: Pass routes/ let other routes than root handle by the Single page application Node.js和Express 4如何分开? - How to separate routes on Node.js and Express 4? 如何在 node.js 和 express 中导出路由? - How to export routes in node.js and express? 如何启用调试 express.js/node.js 应用程序 - How to enable debugging express.js/node.js application 将Angular与Express-Handlebars一起用于Node.js应用程序 - Using Angular with Express-Handlebars for a Node.js application 将node.js应用程序部署到弹性beantalk(使用express) - Deploy node.js application to elastic beanstalk (using express) 如何增加基于express的node.js应用程序的代码覆盖率? - How to increase the code coverage of express based node.js application? 并发如何在Node.js + Express应用程序中工作? - How does concurrency work in Node.js + Express application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM