Features
Micro.
Elegant.
Fast.
Lightweight.
Expressive.
Installation
$ npm install trek-engine
Hello Trek Engine
const co = require('co')
const Engine = require('trek-engine')
const app = new Engine()
// middleware
app.use((ctx, next) => {
// return promise
return next()
})
// async/await
app.use(async (ctx, next) => {
await next()
})
// generation
app.use(co.wrap(function * (ctx, next) {
yield next()
}))
// response
app.use({ res } => {
res.end('Hello Koa')
})
app.run(3000)
— 华丽的分割线!
“这个算是一个 koa 修改进化版!”
有几个特点:
- 中间件使用了生成器,看着这里:https://github.com/trekjs/middleware
- 去掉了 koa 中的
delegates
模块,速度明显提升! - 有个开关
app.raw
你可以使用原生req
和res
进行操作。 - 大量使用新的语法特性(当然这不是什么新鲜事)。
- 轻量,简洁,优雅(async/await)
目前正在基于
trek-engine
开发 trek框架(实际上是重构,2015 年是基于 koa,现在有自己的轮子了, 哈哈).
说了这么多,性能如何?
https://travis-ci.org/trekjs/engine
Benchmarks
Framework | Version |
---|---|
Express | 4.14.0 |
Koa | 2.0.0-alpha.6 |
Toa | 1.8.11 |
Trek Engine | - |
Frameworks’ Battle! :fire: :collision:
$ make battle
5 middleware
5 middleware
------- express -------
Hello World
6114.56
5 middleware
------- koa -------
Hello World
6613.55
5 middleware
------- toa -------
Hello World
5142.69
5 middleware
------- trek -------
Hello World
7393.59
15 middleware
15 middleware
------- express -------
Hello World
5681.79
15 middleware
------- koa -------
Hello World
6822.63
15 middleware
------- toa -------
Hello World
5336.33
15 middleware
------- trek -------
Hello World
8198.41
30 middleware
30 middleware
------- express -------
Hello World
5168.10
30 middleware
------- koa -------
Hello World
6205.17
30 middleware
------- toa -------
Hello World
4727.22
30 middleware
------- trek -------
Hello World
7982.35
50 middleware
50 middleware
------- express -------
Hello World
5393.90
50 middleware
------- koa -------
Hello World
5974.12
50 middleware
------- toa -------
Hello World
4261.97
50 middleware
------- trek -------
Hello World
6634.71
当然也可以执行下面命令,本地测试
$ git clone https://github.com/trekjs/engine
$ npm install
$ npm run bench
Enjoy!