Last Updated: January 28, 2019
·
1.07K
· frostwind

Koa with coffeescript

Recently, i wanted to try koa.js, but the problem is that generators are not supported in coffee-script yet.

To make them work you have to install coffee-script from github using this command :

npm install jashkenas/coffeescript

And since coffee-script need to detect the yield keyword to know that we are declaring a generator and not a regular function, you have to tune your code a bit.

Let's take koa's hello world for example :

Original:

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

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

app.listen(3000);

Coffee-script version:

koa = require 'koa'
app = koa()

app.use (next) ->
  yield next
  this.body = 'Hello World'

app.listen 3000

Have fun experimenting with Koa :)