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.