我想用它的方法类型获取所有.js文件的操作/路由。基本上,我想要创建一个函数,返回所有的操作和方法。
router.get('/this/is/action', function (req, res, next) {
// This action is in login.js
}
router.post('/another/action1', function (req, res, next) {
// This action is in signup.js
}因此,在这种情况下,我的函数必须返回如下所示的响应。
{
"data":[
{
"url":"/this/is/action",
"method":"get"
}
{
"url":"/another/action1",
"method":"post"
}
]
}发布于 2017-10-11 11:34:43
如果使用的是express,则可以使用速递的杜格沃森创建者创建的以下代码段。
function print (path, layer) {
if (layer.route) {
layer.route.stack.forEach(print.bind(null, path.concat(split(layer.route.path))))
} else if (layer.name === 'router' && layer.handle.stack) {
layer.handle.stack.forEach(print.bind(null, path.concat(split(layer.regexp))))
} else if (layer.method) {
console.log('%s /%s',
layer.method.toUpperCase(),
path.concat(split(layer.regexp)).filter(Boolean).join('/'))
}
}
function split (thing) {
if (typeof thing === 'string') {
return thing.split('/')
} else if (thing.fast_slash) {
return ''
} else {
var match = thing.toString()
.replace('\\/?', '')
.replace('(?=\\/|$)', '$')
.match(/^\/\^((?:\\[.*+?^${}()|[\]\\\/]|[^.*+?^${}()|[\]\\\/])*)\$\//)
return match
? match[1].replace(/\\(.)/g, '$1').split('/')
: '<complex:' + thing.toString() + '>'
}
}
app._router.stack.forEach(print.bind(null, []))https://stackoverflow.com/questions/46687067
复制相似问题