造了个小轮子,支持嵌套和中间件调用的Router
发布于 8 年前 作者 NameIsUseless 2904 次浏览 来自 分享

核心代码在这里,然后自己封装了一个koa中间件

支持嵌套路由和链式调用:

// Nested routes
let admin_router = router.route('/admin', function *(next) {
    let hasLogin = yield this.checkSession() // Check session somehow

    if (hasLogin) {
        yield* next
    } else {
        this.redirect(url_to_login)
    }
})

admin_router.route('/::', function *(next, method) {
    this.body = `Admin's ${method} operating`
})

虽然router模块以及有很多了,不过造轮子还是挺好玩的o( ̄▽ ̄)d

回到顶部