Creating a Koa web app
Koa is an evolution the middleware concept in line with updates to the JavaScript language. Originally in Koa-v1, flow control was handled by re-purposing ECMAScript 2015 (ES6) Generator functions (using the yield
keyword to freeze function execution) combined with promises. In Koa-v2, a more normative is taken using ES 2016 async/await
syntax.
It's a minimalist web framework compared to Express (and far more minimalist compared to Hapi). Koa is more closely comparable to the Connect web framework (which was the precursor to Express). This means that functionality that tends to come as standard in other web frameworks (such as route handling) is installed separately as Koa middleware.
In this recipe, we're going to create a Koa (v2) web application.
Note
Node 8+ Only
This recipe focuses on Koa-v2, using up-to-date async/await
syntax, which is only supported from Node 8 onwards.
Getting ready
Let's create a folder called app
, initialize it as a package, and install koa
,...