Closed i5ting closed 6 years ago
Hi! This router aims to be framework independent, so it uses the defaults nodejs request and response object.
If you want to add support for koa, probably an external lib that depends on find-my-way
could be the best option.
@delvedor https://github.com/moajs/moa-router#usage support koa/express/http
$ autocannon 127.0.0.1:3000/test
QPS
a better find-my-way(support for koa/express/http)
This is not correct. You have copy pasted the code and remove all the contributions of other developers. That is not a nice thing to do in the open source world.
What I was suggesting was create a router and declare find-my-way
as dependency, then use the find-my-way
apis to reach your goal.
i'm sorry,it‘s really a quick and dirty way,i wanna show a better way for design,we only need a little code to achive the goal
@delvedor it's greate for you impl radix-tree。maybe find-my-way can be more compatibility & friendly to other web framework
@delvedor i will be refact my code later~thanks
'use strict'
const httpMethods = require('./methods')
const Router = require('find-my-way')
class MoaRouter extends Router {
constructor (opts) {
super(opts)
let self = this
httpMethods.forEach(function (method) {
self[method] = function (path, handler, store) {
return self.on(method.toUpperCase(), path, handler, store)
}
})
}
lookupKoa (ctx, next) {
let req = ctx.req
// var res = ctx.res
let handle = this.find(req.method, sanitizeUrl(req.url))
if (!handle) return this._defaultKoaRoute(ctx, next)
ctx.params = handle.params
ctx.store = handle.store
return handle.handler(ctx, next)
}
lookupExpress (req, res, next) {
let handle = this.find(req.method, sanitizeUrl(req.url))
if (!handle) return this._defaultHttpRoute(req, res)
req.params = handle.params
req.store = handle.store
return handle.handler(req, res, next)
}
lookupHttp (req, res) {
let handle = this.find(req.method, sanitizeUrl(req.url))
if (!handle) return this._defaultHttpRoute(req, res)
return handle.handler(req, res, handle.params, handle.store)
}
_defaultKoaRoute (ctx, next) {
if (this.defaultRoute) {
this.defaultRoute(ctx, next)
}
}
_defaultHttpRoute (req, res) {
if (this.defaultRoute) {
this.defaultRoute(req, res)
} else {
res.statusCode = 404
res.end()
}
}
routes () {
let router = this
if (router.type === 'express') {
return function (req, res, next) {
router.lookupExpress(req, res, next)
}
} else if (router.type === 'koa') {
return function (ctx, next) {
router.lookupKoa(ctx, next)
}
} else {
return function (req, res) {
router.lookupHttp(req, res)
}
}
}
}
module.exports = function (opts) {
if (opts && !opts.type) opts.type = 'koa'
return new MoaRouter(opts)
}
function sanitizeUrl (url) {
for (let i = 0, len = url.length; i < len; i++) {
let charCode = url.charCodeAt(i)
if (charCode === 63 || charCode === 35) {
return url.slice(0, i)
}
}
return url
}
@delvedor and the desc now is a better router base on find-my-way(support koa/express/http) https://github.com/moajs/moa-router
Good job! :)
code
https://github.com/moajs/moa-router