gpt4 book ai didi

node.js - 如何在 Express 中获取所有已注册的路线?

转载 作者:IT老高 更新时间:2023-10-28 21:45:59 26 4
gpt4 key购买 nike

我有一个使用 Node.js 和 Express 构建的 Web 应用程序。现在我想列出所有已注册的路线及其适当的方法。

例如,如果我执行了

app.get('/', function (...) { ... });
app.get('/foo/:id', function (...) { ... });
app.post('/foo/:id', function (...) { ... });

我想检索一个对象(或与之等效的对象),例如:

{
get: [ '/', '/foo/:id' ],
post: [ '/foo/:id' ]
}

这可能吗?如果可以,怎么做?

最佳答案

表达 3.x

好的,我自己找到的......它只是 app.routes :-)

表达 4.x

Applications - 使用 express()

构建

app._router.stack

Routers - 使用 express.Router()

构建

router.stack

注意:堆栈也包含中间件功能,应过滤以仅获取“路由”

关于node.js - 如何在 Express 中获取所有已注册的路线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14934452/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com