As you may know, Strapi is built on top of Koa, a new framework for Node.js web applications. This article summaries the most important and interesting reasons why we use Koa for our stack.
Koa is a new web framework designed by the team behind Express and Connect, which aims to be a smaller, more expressive, and more robust foundation for web applications and APIs. Through leveraging ES2015 generators Koa allows you to ditch callbacks and greatly increase error-handling. Let's dig in those interesting points.
A Koa application is an object containing an array of middleware generator functions which are composed and executed in a stack-like manner upon request. Koa is similar to many other middleware systems that you may have encountered such as Ruby's Rack, Connect, and so on. However a key design decision was made to provide high level 'sugar' at the otherwise low-level middleware layer. This improves interoperability, robustness, and makes writing middleware much more enjoyable.
In fact, despite supplying a reasonably large number of helpful methods, Koa maintains a small footprint, as no middleware are bundled. Instead it provides an elegant suite of methods such as content-negotiation, redirections, proxy support etc., giving you ease and speed of development along with the granular control over your Node.js application. This approach is good because it allows you to pick and choose modules that are most important to you. Express has made strides to separate out middleware as well, but Koa has been designed with this mindset, and it plays to its favor.
Thanks to this granularity, Koa 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.
Koa takes an interesting approach to contexts. It encapsulates Node.js request and response objects into a single object. There's a ton of helper methods for building out great APIs, which is a huge plus. Ultimately, this approach simplifies requests and responses in general.
Koa middleware cascade in a more traditional way as you may be used to with similar tools - this was previously difficult to make user friendly with Node.js' use of callbacks. However with generators we can achieve 'true' middleware. Contrasting Connect's implementation which simply passes control through series of functions until one returns, Koa yields 'downstream', then control flows back 'upstream'. When a middleware invokes 'yield next' the function suspends and passes control to the next middleware defined. After there are no more middleware to execute downstream, the stack will unwind and each middleware is resumed to perform its upstream behaviour.
For any Node.js developer, Koa is going to demand you to think differently. This is a really good thing in the long run: Koa completely ditches callbacks, and instead opts to use generators from the upcoming ES2015 release.
Koa is currently the only framework that plans to put ES2015 to good use. It has made some great design decisions: its lightweight, it avoids nested callbacks, and it is uniquely suited for modern API development especially when it comes to database queries (automatic parallel execution of queries using the yield keyword). Koa is a great framework that improves productivity at the expense of making you think differently as a Node.js developer.
Whether you are a experienced or a new Node.js developer, I highly encourage you to try and write a simple API using Strapi. Strapi is an open-source Node.js rich framework built on top of Koa for building applications and services. Its ensemble of small modules work together to provide simplicity, maintainability, and structural conventions to Node.js applications. Strapi comes with a convenient command-line tool to quickly get your application scaffolded and running in a few seconds.