Koa 还是 Express
為什么80%的碼農都做不了架構師?>>> ??
先放一些別人寫的
- http://yemista.com/koa-js-vs-express-js-which-one-better-for-node-js/
- https://www.airpair.com/node.js/posts/nodejs-framework-comparison-express-koa-hapi
- https://cnodejs.org/topic/540335d0cd66f2eb379b21d5
我也來湊個熱鬧
群里很多人在問到底該用Koa還是express,本文會對比2個框架的各種細節,并給出指導意見,希望能夠為大家解惑。
- http://koajs.com/
- http://expressjs.com/
koa
koa 是由 Express 原班人馬打造的,致力于成為一個更小、更富有表現力、更健壯的 Web 框架。使用 koa 編寫 web 應用,通過組合不同的 generator,可以免除重復繁瑣的回調函數嵌套,并極大地提升錯誤處理的效率。koa 不在內核方法中綁定任何中間件,它僅僅提供了一個輕量優雅的函數庫,使得編寫 Web 應用變得得心應手。
版本要求
Koa 目前需要 >=0.11.x版本的 node 環境。并需要在執行 node 的時候附帶 --harmony 來引入 generators 。
express無所謂,目前0.10+都ok,甚至更低版本
中間件變化
Koa 應用是一個包含一系列中間件 generator 函數的對象。 這些中間件函數基于 request 請求以一個類似于棧的結構組成并依次執行。 Koa 類似于其他中間件系統(比如 Ruby’s Rack 、Connect 等), 然而 Koa 的核心設計思路是為中間件層提供高級語法糖封裝,以增強其互用性和健壯性,并使得編寫中間件變得相當有趣。
Koa 包含了像 content-negotiation(內容協商)、cache freshness(緩存刷新)、proxy support(代理支持)和 redirection(重定向)等常用任務方法。 與提供龐大的函數支持不同,Koa只包含很小的一部分,因為Koa并不綁定任何中間件。
和 express 基于的中間件Connect,差別并不大,思想都是一樣的,它里面說的
增強其互用性和健壯性我還沒玩出太多感想,請大家指點
除了yield next;外,并無其他
yield要說一下,必須在處理的中間件里才會回調
比如
var koa = require('koa'); var app = koa();//1 x-response-timeapp.use(function *(next){var start = new Date;yield next;var ms = new Date - start;this.set('X-Response-Time', ms + 'ms'); });//2 loggerapp.use(function *(next){var start = new Date;yield next;var ms = new Date - start;console.log('%s %s - %s', this.method, this.url, ms); });//3 responseapp.use(function *(){this.body = 'Hello World'; });app.listen(3000);在程序啟動的時候,1和2是沒有執行的,只有當執行到任意請求,比如3的時候,它才會調用1和2
錯誤處理
koa
app.on('error', function(err){log.error('server error', err); });而在新版的express里
server.on('error', onError);/*** Event listener for HTTP server "error" event.*/function onError(error) {if (error.syscall !== 'listen') {throw error;}var bind = typeof port === 'string'? 'Pipe ' + port: 'Port ' + port;// handle specific listen errors with friendly messagesswitch (error.code) {case 'EACCES':console.error(bind + ' requires elevated privileges');process.exit(1);break;case 'EADDRINUSE':console.error(bind + ' is already in use');process.exit(1);break;default:throw error;} }二者實際上沒有啥大差別
Koa Context
將 node 的 request 和 response 對象封裝在一個單獨的對象里面,其為編寫 web 應用和 API 提供了很多有用的方法。
這些操作在 HTTP 服務器開發中經常使用,因此其被添加在上下文這一層,而不是更高層框架中,因此將迫使中間件需要重新實現這些常用方法。
context 在每個 request 請求中被創建,在中間件中作為接收器(receiver)來引用,或者通過 this 標識符來引用:
app.use(function *(){this; // is the Contextthis.request; // is a koa Requestthis.response; // is a koa Response });比express里爽一些,express里中間件可變參數還是會比較惡心,而且性能也不好
對比一些api
- req
- res
基本上一模一樣
二者比較總結
https://github.com/koajs/koa/blob/master/docs/koa-vs-express.md
| Middleware Kernel | ? | ? | ? |
| Routing | ? | ? | ? |
| Templating | ? | ? | ? |
| Sending Files | ? | ? | ? |
| JSONP | ? | ? | ? |
Does Koa replace Express?
It’s more like Connect, but a lot of the Express goodies were moved to the middleware level in Koa to help form a stronger foundation. This makes middleware more enjoyable and less error-prone to write, for the entire stack, not just the end application code.
Typically many middleware would re-implement similar features, or even worse incorrectly implement them, when features like signed cookie secrets among others are typically application-specific, not middleware specific.
拿koa來比較express并不太合適,可以說它是介于connect和express中間的框架
- 與connect類似都是調用棧思想,但修改了中間件模式,使用generator
- 把express里的一些好的東西加進去,但剔除了路由,視圖渲染等特性
總結
koa是一個比express更精簡,使用node新特性的中間件框架,相比之前express就是一個龐大的框架
- 如果你喜歡diy,很潮,可以考慮koa,它有足夠的擴展和中間件,而且自己寫很簡單
- 如果你想簡單點,找一個框架啥都有,那么先express
koa是大勢所趨,我很想用,但我目前沒有選koa,我的考慮
- node 0.11+我目前沒有搞定node-inspector,所以我啥時候搞定,啥時候遷移
- 團隊成本問題,如果他們連express都不會,上來就koa,學習曲線太陡,不合適
- 目前基于express的快讀開發框架需要一段時間遷移到koa
和es6的考慮是一樣的,又愛又恨,先做技術儲備,只要時機ok,毫不猶豫的搞起。
目前express由strongloop負責,它的下一步如何發展,還說不好,比如5.0、6.0是否會用koa作為中間件也不好說
koa代碼很少,可以很容易讀完
https://github.com/koajs/koa
另外值得提得一點是,核心開發者 @dead-horse 是阿里的員工,贊一下國內的開源。
12月份我寫了一個koa-generator,可以讓很多習慣express的人遷移過來,見文章https://cnodejs.org/topic/56650091e7cd33da066d6ee7
轉載于:https://my.oschina.net/dawd/blog/744332
總結
以上是生活随笔為你收集整理的Koa 还是 Express的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 社保卡里的钱怎么转账
- 下一篇: DSP 投放的基本流程和算法