Koa vs Express

来源:互联网 发布:iwatch软件排行 编辑:程序博客网 时间:2024/06/14 20:56

点击这里查看原文

Koa vs Express

在价值观上, Koa 宣称”fix and replace node”,而Express则为”augments node”。Koa 通过co库摆脱回调的坑并且简化异常处理。

它通过暴露出自己的this.requestthis.response对象代替node自身的reqres 对象。相反的,Express为node自身的reqres 对象添加附加的属性和方法,并且包含了许多的框架特性。比如,路由和模板。

因此,Koa可以看作是node.js的http 模块的抽象,Express 是node.js的一个应用框架。

特点 Koa Express Connect 中间件 ✓ ✓ ✓ 路由 ✓ 模板 ✓ 文件传送 ✓ JSONP ✓

因此,如果你喜欢node.js和原生node.js风格的代码,你也许会坚持用Connect/Express 或者更加轻量级的框架。如果期望玩转generators,那就用Koa吧。

基于这个不同,传统的node.js中间件(形如 (req, res, next) 的函数)和Koa不兼容。你的应用可能会整个重写。

Koa 会代替 Express?

它更像Connect,但是许多的Express 黑科技都转变成了帮助Koa形成强大接触的中间件。这使得编写全栈中间件更加随意,而不仅仅是后端代码。

特别是很多中间件会重复实现相同的特性,更糟糕的是错误的实现它们。例如类似signed cookie secrets的特性是典型的属于应用而不是中间件。

Koa 会代替Connect?

generators 是的我们在代码中用更少的回调,Connect 同样有能力,有些人可能还是喜欢它,这取决于你喜欢什么。

为什么不用Koa 而是Express 4.0?

Koa is a pretty large departure from what people know about Express,
the design is fundamentally much different, so the migration from
Express 3.0 to this Express 4.0 would effectively mean rewriting
the entire application, so we thought it would be more appropriate
to create a new library.

How is Koa different than Connect/Express?

Generator-based control flow

Thanks to co.

No callback hell.

Better error handling through try/catch.

No need for domains.

Koa is barebones

Unlike both Connect and Express, Koa does not include any middleware.

Unlike Express, routing is not provided.

Unlike Express, many convenience utilities are not provided. For example, sending files.

Koa is more modular.

Koa relies less on middleware

For example, instead of a “body parsing” middleware, you would instead use a body parsing function.

Koa abstracts node’s request/response

Less hackery.

Better user experience.

Proper stream handling.

0 0
原创粉丝点击