Skip to content

Instantly share code, notes, and snippets.

@rtablada
Last active November 2, 2018 14:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rtablada/0124c4191131bd67d03d14386db198e9 to your computer and use it in GitHub Desktop.
Save rtablada/0124c4191131bd67d03d14386db198e9 to your computer and use it in GitHub Desktop.
Difference between Koa, Express, and Hapi

Express and Koa are more 1-1 comparisons and HAPI is slightly different so I’ll explain the diffs for those first.

Express and Koa are both middleware driven server libraries. With middleware you’re defining a set of functions to handle an incoming request and either return a result from that function or make some changes to the incoming request (or outgoing response) and pass on to the next set of middleware (think each middleware function as a colored lens letting light through or a mirror reflecting light back).

The big difference between the two is how they setup middleware, call middleware, and deal with async behavior (and the resulting errors or responses).

Express is based on callbacks so async actions will need to use callbacks as a control flow. While you can use async/await or generators in Express middleware the library itself does not handle this and may exit early or hang (there are some middleware that can be added that add more flexibility for async/await to Express but they aren’t 1st party supported). Koa on the other hand is built from the ground up to work with async/await or generators for control flow and error handling. This means that Koa middleware are almost always declared as async functions or generators with proper handling of thrown errors or awaited promises using built in language features rather than extra functions and/or callbacks.

It should be noted that Express provides a routing API as well as this middleware architecture out of the box. Express itself it built on the "Connect" middleware library and adds routing and some extra simplifications for most major usecases. Koa does not include routing out of the box, however the koa-router middleware library is well supported and has similar developer APIs to the Express router.

Hapi is slightly different than Koa and Express since it is a bit more of a framework rather than a low level middleware or router. Out of the box Hapi includes a lot of common features such as authentication, views, validations, and more: in Koa or Express these usually are added via optional middleware. Hapi also has a plugin system for abstracting or sharing functionality. While under the hood Hapi does use a middleware system this is a bit more abstracted than in Koa or Express and the developer ergonomics are a bit different favoring route and method based functions over a more layered middleware stack.

If you're interested in learning how to put Koa into action and see how a Koa API can be architected and used along with a single page application check out the "Full Stack Ember and Node.js" course on Embercasts where we build a Koa based JSON API with multiple related resources.

@lowderdev
Copy link

Thanks for this, Ryan! Very helpful. I will pass it along to my team.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment