delvedor / find-my-way

A crazy fast HTTP router
MIT License
1.49k stars 139 forks source link

add koa && express support #42

Closed i5ting closed 6 years ago

i5ting commented 6 years ago

code

const http = require('http')
const Koa = require('koa');
const app = new Koa();

const router = require('./')()

router.get('/', (ctx, next) => {
  ctx.body = {'path': 'root'}
})

router.on('GET', '/test', (ctx, next) => {
  ctx.body = {'hello': 'world'}
})

app.use(router.routes());

app.use(async function (ctx, next) {
  ctx.body = "default"
});

const server = http.createServer(app.callback())

server.listen(3030, err => {
  if (err) throw err
  console.log('Server listening on: http://localhost:3000')
})

https://github.com/moajs/moa-router

delvedor commented 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.

i5ting commented 6 years ago

@delvedor https://github.com/moajs/moa-router#usage support koa/express/http

i5ting commented 6 years ago

Performace

$ autocannon 127.0.0.1:3000/test

QPS

  1. moa-router(http) 28456
  2. moa-router(koa) 17439.6
  3. koa-router 12748.73
  4. moa-router(express) 11779.1
  5. express-router 10374.6
delvedor commented 6 years ago

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.

i5ting commented 6 years ago

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

i5ting commented 6 years ago

@delvedor it's greate for you impl radix-tree。maybe find-my-way can be more compatibility & friendly to other web framework

i5ting commented 6 years ago

@delvedor i will be refact my code later~thanks

i5ting commented 6 years ago
'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
}
i5ting commented 6 years ago

@delvedor and the desc now is a better router base on find-my-way(support koa/express/http) https://github.com/moajs/moa-router

mcollina commented 6 years ago

Good job! :)