Express 獲取全部路徑

這裏寫圖片描述

/* 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}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章