Koa.js : Next Generation Web Framework for Building Node.js Apps Without Callback Hell

I have been following the Node.js platform since 2011 and had been observed that it was difficult to maintain full-fledged web apps on the production servers, mainly due to the lack of error handling capabilities with callback hells. I have been recommending Node.js for building REST APIs and mobile backends and had been feared about developing complex web apps on the production environments.  I have been using Express.js as an application framework building apps, and at the same time, I was looking for a better web application framework. Last year, I had evaluated DerbyJS and MeteorJS. I was bit interested on DerbyJS last year, but the pace of the development of Derby framework was not good. But last month, I have super excited after the initial release of Koa.js. Initially, the most interesting fact was the team behind the framework. Koa was developed by the same time who has created the most popular Node.js framework, Express. TJ Holowaychuk, who is the most prolific author of Node.js modules, is leading the Koa project.

Koa.js – Next Generation Node.js Framework with ECMAScript 6 Goodness

The design goal of the Koa is to provide a framework which is smaller, more expressive using middleware for node.js using generators, and more robust foundation for web applications and APIs. Koa is heavily leveraging JavaScript Generators provided by Harmony (EcmaScript 6). JavaScript Generators is an experimental technology, part of the EcmaScript 6, which has got approved and it is already available in Node.js since v 0.11. By using JavaScript generators, Koa lets you avoid callbacks which will increase the error handling capabilities for your Node.js apps. In order to understand how does Koa eliminates callback, you need a good understanding on JavaScript generators. I highly recommend to have a look on Mozilla Developer Network on Iterators and generators.

A simple hello world program in Koa.js is provided below:

   1:  var koa = require('koa');
   2:  var app = koa();
   3:   
   4:  app.use(function *(){
   5:    this.body = 'Hello World';
   6:  });
   7:   
   8:  app.listen(3000);

To run Koa programs, you must be running node 0.11.9 or higher along with --harmony flag for the JavaScript generator support. The function * denotes that it is a generator function.

node --harmony helloworld.js

The code block below demonstrates how to use generator functions in Koa with JavaScript promises.

   1:  var koa = require('koa');
   2:  var Q = require('q');
   3:  var request = Q.denodeify(require('request'));
   4:  var app = module.exports = koa();
   5:   
   6:  app.use(function *(){
   7:      var response = yield
   8:          getHttpRequest('https://twitter.com/shijucv');
   9:      this.body =  response.body;
  10:  });
  11:   
  12:  function *getHttpRequest(url) {
  13:      var resultParams = yield request(url);
  14:      return resultParams[0];
  15:  }
  16:  app.listen(3000);

The above code block uses JavaScript Promises library Q where we are using denodeify function of Q which creates a promise returning function from a Node style function. The yield is key feature of JavaScript generators, which will pauses the execution of a JavaScript function when it requires to get the result of a function, instead of providing a callback to it. We are working without using callbacks. You can view the above source code from here.

First Impressions about Koa

I am super excited about Koa.js and planning to deeply look on the framework. Koa is really a pragmatic Node.js framework which can be used for building complex web apps on the Node.js platform. I am sure that Koa will be better than Express.js. I really liked the design of Koa which is based on Node.js middleware, but Koa framework is not bundled with middleware. You can use common middleware from Koa-Common. I will definitely use Koa for building productions web apps on the Node.js platform.

Resources

Koa.js web site - http://koajs.com

Github Repository - https://github.com/koajs/koa

Iterators and generators - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators

I have been working for adding few samples for Koa at https://github.com/shijuvar/koa-demos

You can follow me on Twitter @shijucv

4 Comments

Comments have been disabled for this content.