Node.js框架對比: Express vs. Koa vs. Hapi

原文地址疮跑,中文地址koa文檔

Paste_Image.png

1 Introduction

Express.js無疑是當(dāng)前Node.js中最流行的Web應(yīng)用程序框架扇售。它幾乎成為了大多數(shù)Node.js web應(yīng)用程序的基本的依賴,甚至一些例如Sails.js這樣的流行的框架也是基于Express.js。然而你還有一些其他框架的選擇赫冬,可以給你帶來“sinatra”一樣的感覺(譯注:sinatra是一個簡單的Ruby的Web框架,可以參考這篇博文)溃列。另外兩個最流行的框架分別是Koa和Hapi劲厌。
這篇文章不是打算說服你哪個框架比另外一個更好,而是只是打算讓你更好地理解每個框架能做什么听隐,什么情況下一個框架可以秒殺另外一個补鼻。
2 Framework backgrounds

我們將要探討的兩個框架看起來都非常相似。每一個都能夠用幾行代碼來構(gòu)建一個服務(wù)器雅任,并都可以非常輕易地構(gòu)建REST API风范。我們先瞧瞧這幾個框架是怎么誕生的。
2.1 Express

2009年6月26日沪么,TJ Holowaychuk提交了Express的第一次commit硼婿,接下來在2010年1月2日,有660次commits的Express 0.0.1版本正式發(fā)布禽车。TJ和Ciaron Jessup是當(dāng)時最主要的兩個代碼貢獻者寇漫。在第一個版本發(fā)布的時候,根據(jù)github上的readme.md哭当,這個框架被描述成:

Insanely fast (and small) server-side JavaScript web development framework built on node.js and V8 JavaScript engine.

差不多5年的時間過去了猪腕,Express擁有了4,925次commit,現(xiàn)在Express的最新版本是4.10.1钦勘,由StrongLoop維護陋葡,因為TJ現(xiàn)在已經(jīng)跑去玩Go了。
2.2 Koa

大概在差不多一年前的2013年8月17日彻采,TJ Holowaychuk(又是他8汀)只身一人提交了Koa的第一次commit捌归。他描述Koa為“表現(xiàn)力強勁的Node.js中間件,通過co使用generators使得編寫web應(yīng)用程序和REST API更加絲般順滑”岭粤。Koa被標(biāo)榜為只占用約400行源碼空間的框架惜索。Koa的目前最新版本為0.13.0,擁有583次commits剃浇。
2.3 Hapi

2011年8月5日巾兆,WalmartLabs的一位成員Eran Hammer提交了Hapi的第一次commit。Hapi原本是Postmile的一部分虎囚,并且最開始是基于Express構(gòu)建的角塑。后來它發(fā)展成自己自己的框架,正如Eran在他的博客里面所說的:

hapi was created around the idea that configuration is better than code, thatbusiness logic must be isolated from the transport layer...

Hapi最新版本為7.2.0淘讥,擁有3,816次commits圃伶,并且仍然由Eran Hammer維護。

所有開發(fā)者要開發(fā)Node.js web應(yīng)用程序的第一步就是構(gòu)建一個基本的服務(wù)器蒲列。所以我們來看看用這幾個框架構(gòu)建一個服務(wù)器的時候有什么異同窒朋。
3 Creating a server

The first step for any developer when working on a Node.js web application is to create a basic server. So lets create a server using each framework to see their similarities and differences.
4 Routes

Now lets dig into one of the most important features of a server, routing. First lets create the cliche "Hello world" application for each framework and then move on to something a little more useful, REST API.
5 The Good and The Bad

5.1 Express
5.1.1 The Good

Express has the biggest community not only out of the three frameworks compared here but out of all the web application frameworks for Node.js. It is the most matured framework out of the three, with almost 5 years of development behind it and now has StrongLoop taking control of the repository. It offers a simple way to get a server up and running and promotes code reuse with it's built in router.
5.1.2 The Bad

There is a lot of manual tedious tasks involved in Express. There is no built in error handling, it is easy to get lost in all of the middleware that could be added to solve a solution, and there are many ways to do one thing. Express describes itself as being opinionated, this could be good or bad but for beginning developers who must likely chose Express, this is a bad thing. Express also has a larger foot print compared to the other frameworks.
5.2 Koa
5.2.1 The Good

Koa has a small footprint, it is more expressive and it makes writing middleware a lot easier than the other frameworks. Koa is basically a barebone framework where the developer can pick (or write) the middleware they want to use rather than compromising to the middleware that comes with Express or Hapi. It's the only framework embracing ES6, for instance its using ES6 generators.
5.2.2 The Bad

Koa is still unstable and heavily in development. Using ES6 is still ahead of the game for example version 0.11.9+ of Node.js needs to be used to run Koa and right now the latest stable version on Node.js is version 0.10.33. One thing that is in the good but could also be in the bad much like Express is the option of selecting multiple middlewares or writing your own middleware. Such as the router we looked at earlier, there are a lot of middleware routers to handle a variety of options.
5.3 Hapi
5.3.1 The Good

Hapi is proud to say that their framework is based on configuration over code, and a lot of developers would argue that this is a good thing. This is very usual in large teams to add consistency and reusability. Also with the framework being backed by WalmartLabs as well as many other big name companies using Hapi in production, it has been battle tested and companies are confident enough to run their applications off of it. So all signs point towards this project continuing to mature in to a great framework.
5.3.2 The Bad

Hapi definitely seems to be more tailored towards bigger or more complex applications. It is probably a little too much boilerplate code to throw together a simple web app and there is also a lot less examples or open source applications that use hapi. So choosing it might involve a little more part on the developer rather than using third party middleware.
6 Summary

We have seen some good but practical examples of all three frameworks. Express is definitely the most popular and most recognized framework of the three. It is almost a reaction to first create a server using Express when starting new development on an application but hopefully now there might be some thought involved whether to use Koa or Hapi as an alternative. Koa shows real promise for the future and is ahead of the pack with embracing ES6 and the web component ideology that the web development community is moving towards. Hapi should be the first consideration for large teams and large projects. It pushes for configuration over code which almost always benefits teams and the re-usability most teams strive towards. Now go out and try a new framework, maybe you'll love it, maybe you'll hate, but you will never know and in the end, it will make you a better developer.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市蝗岖,隨后出現(xiàn)的幾起案子侥猩,更是在濱河造成了極大的恐慌,老刑警劉巖剪侮,帶你破解...
    沈念sama閱讀 221,820評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件拭宁,死亡現(xiàn)場離奇詭異,居然都是意外死亡瓣俯,警方通過查閱死者的電腦和手機杰标,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,648評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來彩匕,“玉大人腔剂,你說我怎么就攤上這事⊥找牵” “怎么了掸犬?”我有些...
    開封第一講書人閱讀 168,324評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長绪爸。 經(jīng)常有香客問我湾碎,道長,這世上最難降的妖魔是什么奠货? 我笑而不...
    開封第一講書人閱讀 59,714評論 1 297
  • 正文 為了忘掉前任介褥,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘柔滔。我一直安慰自己溢陪,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 68,724評論 6 397
  • 文/花漫 我一把揭開白布睛廊。 她就那樣靜靜地躺著形真,像睡著了一般。 火紅的嫁衣襯著肌膚如雪超全。 梳的紋絲不亂的頭發(fā)上咆霜,一...
    開封第一講書人閱讀 52,328評論 1 310
  • 那天,我揣著相機與錄音卵迂,去河邊找鬼裕便。 笑死,一個胖子當(dāng)著我的面吹牛见咒,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播挂疆,決...
    沈念sama閱讀 40,897評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼改览,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了缤言?” 一聲冷哼從身側(cè)響起宝当,我...
    開封第一講書人閱讀 39,804評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎胆萧,沒想到半個月后庆揩,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,345評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡跌穗,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,431評論 3 340
  • 正文 我和宋清朗相戀三年订晌,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片蚌吸。...
    茶點故事閱讀 40,561評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡锈拨,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出羹唠,到底是詐尸還是另有隱情奕枢,我是刑警寧澤,帶...
    沈念sama閱讀 36,238評論 5 350
  • 正文 年R本政府宣布佩微,位于F島的核電站缝彬,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏哺眯。R本人自食惡果不足惜谷浅,卻給世界環(huán)境...
    茶點故事閱讀 41,928評論 3 334
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧壳贪,春花似錦陵珍、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,417評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至磕蒲,卻和暖如春留潦,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背辣往。 一陣腳步聲響...
    開封第一講書人閱讀 33,528評論 1 272
  • 我被黑心中介騙來泰國打工兔院, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人站削。 一個月前我還...
    沈念sama閱讀 48,983評論 3 376
  • 正文 我出身青樓坊萝,卻偏偏與公主長得像,于是被迫代替她去往敵國和親许起。 傳聞我的和親對象是個殘疾皇子十偶,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,573評論 2 359

推薦閱讀更多精彩內(nèi)容