Express 获取全部路径

版权声明:***本文为博主原创文章,未经博主允许不得转载。*** https://blog.csdn.net/rrrrrr123rrr/article/details/78488685

这里写图片描述

/* app.js */
app.use('/', index);
index.app = app

/* index.js */
router.get('/', function(req, res, next) {
    var urls = []

    parseHandle("", urls, router.app._router)

    res.render('index', {
        title: 'Express',
        urls: urls
    });
});

function parseHandle(prefix, urls, handle) {
    if (!handle) return
    handle.stack.forEach((layer) => {
        if (layer.name == "router") {
            var llPrefix = prefix
            var matchs = layer.regexp.toString().match(/\\(\/[^\/\?]*)\\\//)
            if (matchs) {
                llPrefix += matchs[1]
            }
            parseHandle(llPrefix, urls, layer.handle)
        }
        if (layer.name == "bound dispatch") {
            urls.push(prefix + layer.route.path)
        }
    })
}
  • index.jade
extends layout

block content
  h1= title
  p Welceme to #{title}

  h2 URL 列表
  each item in urls
    li
      a(href='#{item}') #{item}

猜你喜欢

转载自blog.csdn.net/rrrrrr123rrr/article/details/78488685