Node.js framework - Koa

in node •  8 years ago  (edited)

node.js framework - Koa

http://koajs.com/

Framework backgrounds:
The initial commit for Koa was just over a year ago on August 17, 2013 by none other than TJ Holowaychuk. He describes it as "Expressive middleware for node.js using generators via co to make writing web applications and REST APIs more enjoyable to write". Koa is advertised as having a small footprint of ~400 SLOC. It is now on version 0.13.0 with 585 commits.

Creating a server:
The initial commit for Koa was just over a year ago on August 17, 2013 by none other than TJ Holowaychuk. He describes it as "Expressive middleware for node.js using generators via co to make writing web applications and REST APIs more enjoyable to write". Koa is advertised as having a small footprint of ~400 SLOC. It is now on version 0.13.0 with 585 commits.

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

app.use(function *() {
this.body = 'Hello world';
});

var server = app.listen(3000, function() {
console.log('Koa is listening to http://localhost:3000');
});
Koa is slightly different than Express, it is using ES6 generators. Any function preceded by a * means the function will return a generator object. Basically these generators yield values synchronously but that is beyond the scope of this post. Within the app.use() the generator function sets the response body. In Koa the Context which is equivalent to the this identifier is an encapsulation of node's request and response objects. this.body is a method in the Koa Response object. this.body can be set to a string, buffer, stream, object, or null. This example we used one of the few middlewares provided in the Koa core. This middleware we used catches all routes and responds with the string provided.

REST API:
var koa = require('koa');
var route = require('koa-route');
var app = koa();

// REST API
app.use(route.get('/api/items', function() {
this.body = 'Get';
}));
app.use(route.get('/api/items/:id', function
(id) {
this.body = 'Get id: ' + id;
}));
app.use(route.post('/api/items', function() {
this.body = 'Post';
}));
app.use(route.put('/api/items/:id', function
(id) {
this.body = 'Put id: ' + id;
}));
app.use(route.delete('/api/items/:id', function*(id) {
this.body = 'Delete id: ' + id;
}));

// all other routes
app.use(function *() {
this.body = 'Hello world';
});

var server = app.listen(3000, function() {
console.log('Koa is listening to http://localhost:3000');
});
It's pretty obvious that Koa doesn't have the ability to reduce the repetitive route verbs like Express. It also requires a separate middleware to handle routes. I chose to use koa-route because it is maintained by the Koa team but there are a lot of routes available to use by other maintainers. The routes are very similar to Express with using the same keywords for their method calls like .get(), .put(), .post(), and .delete(). One advantage Koa has with handling its routes, is that it is using the ES6 generator functions which helps reduce the handling of callbacks.

the Good:
Koa has a small footprint, it 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. It's the only framework embracing ES6, for instance its using ES6 generators.

the Bad:
Koa is still unstable and heavily in development. Using ES6 is still ahead of the game for example version 0.11.9+ of Node.js needs to be used to run Koa and right now the latest stable version on Node.js is version 0.10.33. One thing that is in the good but could also be in the bad much like Express is the option of selecting multiple middlewares or writing your own middleware. Such as the router we looked at earlier, there are a lot of middleware routers to handle a variety of options.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

interesting, never used koa! will have to look into this for some musicoin apis we're developing

Hi! I am a robot. I just upvoted you! I found similar content that readers might be interested in:
https://www.airpair.com/node.js/posts/nodejs-framework-comparison-express-koa-hapi