BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Koa Web Framework 0.2.0 Release

Koa Web Framework 0.2.0 Release

This item in japanese

The NodeJS based Koa web application framework has released version 0.2.0. Koa is the successor of the popular Express MVC platform, but relies heavily on newer ES6 constructs. Project lead TJ Holowaychuck describes Koa as “a chance to take what I learned from Connect/Express and do things ‘right’ this time.”

This release is dubbed "short but sweet", and is marked as an important one in that that it reaffirms the team’s design choices from the initial 0.1.0 release, solidifying Koa's API for future releases and production use.

0.2.0 Changes

The biggest update in this release is actually to the koa-compose module, which allows developers to debug requests that are being passed to middleware by logging the content of the request to standard out (stdout) both before and after the middleware has manipulated it.

Some additional smaller changes include routing of socket errors to prevent crashing the node server since sockets are handled at the Node level, and a refactoring of the functionality that is currently shared between Express and Koa into modules that both frameworks can use. An example of this is the “accepts” module, which does content negotiation, allowing the server to respond to requests with different types of content based on the value of the Accepts HTTP Headers.

Built on Generators

Koa calls itself a "next generation web framework" and leverages the co library, which uses generators from the ECMAScript 6 language specification (also known as "Harmony") to create non-blocking synchronous processing for Node. Prior Node frameworks have relied on callbacks and promises to achieve the same sort of "stack processing" that is required for HTTP Requests.

While generators are really a “factory” for creating Harmony iterators, Koa uses them to turn functions into synchronous operations. A Koa app can pass requests through several layers of middleware. Each invoked middleware function must yield its result before the caller will continue.

var koa = require('koa');
var app = koa();

app.use(route.get('/', google));

function *people() {
   // “get” is an asynchronous HTTP call
   var result = yield get('http://www.google.com');
   // this line will not execute until the above yield returns
   this.body = result;

}

No Middleware

Koa includes no middleware itself, allowing its footprint to stay light. "We had originally bundled a lot of middleware into Connect for convenience, not only for the end-user, but for ourselves since Node and the entire eco-system was changing so rapidly, it made maintenance easier. Fast forward a few years and most people agree that bundling them was a mistake", explained Holowaychuk. Holowaychuk went on to say that this realization led to the decision not to bundle any middleware with Koa, but to provide it via separate modules that can be bundled for convenience.

The koa-common module bundles most of the commonly required middleware for a web application. A developer can add all of this middleware to their Koa application via NPM.

$ npm install koa-common

The Future of Koa And Express

Holowaychuk mentioned that Koa is considered to be in a finished state barring the odd feature request now and again.

User “deif” had concerns about the future of Express given the release of Koa.

I have some questions about this:

  1. The FAQ gives a political answer about the status of Express but I imagine that Express will not be actively maintained any more. Correct?
  2. If focus is now on Koa, why the name change from Express when it is already a huge name for node frameworks?
  3. If a new developer sees Express and Koa, would they immediately know which one is being focused on?

Basically I'm wondering why it couldn't be called Express 3.0 (or 4.0).

Holowaychuk explained the name change saying

The migration path from Express to Koa is non-trivial, even though they look similar they're fundamentally quite different so I figured instead of calling it Express 4.0 we would be best to give it a new name…there are still several people maintaining Express, and we're open to adding more people to the team if they're interested!

Rate this Article

Adoption
Style

BT