Tuesday, August 5, 2014

Running Koa.Js in Cloud9

What is Koa.Js?

Koa is a new web framework designed by the team behind Express, which aims to be a smaller, more expressive, and more robust foundation for web applications and APIs. Through leveraging generators Koa allows you to ditch callbacks and greatly increase error-handling. Koa does not bundle any middleware within core, and provides an elegant suite of methods that make writing servers fast and enjoyable.
[Source: koa.js.com – Introduction]

What is Cloud9?

Cloud9 combines a powerful online code editor with a full Ubuntu workspace in the cloud.
[Source: c9.io]

Koa.Js in Cloud9…

As stated by Koa.Js it makes use of the new generator feature in javascript ECMA 6. This feature is support for node from 0.11 and up by toggling the –-harmony flag. By default cloud9 runs in node version 0.10.x or 0.8.x, depending on your setup. You can check this by entering the following command into your bash command line tool in your c9 workspace:

node -v

To resolve this problem we first need to install node version 0.11.x, this can be done by executing the following command:

nvm install 0.11

After this we still have to tell the current workspace to use this version. To make this change type in the following command ( x is the patch number, it should have been shown to you through the previous command ) :

nvm use v0.11.x

Now we can create a simple Koa.Js server ( create an index.js file in your root folder ):

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

app.use(function *(){
this.body = '<h1>Hello from Koa.Js</h1>';
});

app.listen(process.env.PORT);

If you now press the run command, you will be confronted by errors. The reason for this is that the runner/debugger defaults to ‘Node (default)’. This implies that c9 is running your javascript in the default node version 0.10.x or 0.8.x.
To get around this problem, we need to create a run file so that c9 knows how to run node in harmony mode and thereby supporting generators.


If you check ‘Show Hidden Files’ in your workspace directory tree ( the small gear in the upper right corner) , you’ll see a .c9 folder, in that folder there is an other folder called ‘runners’. Create a new file in that folder called ‘Node 0.11.x.run’ and open it. Next copy past the following code into it and save:

// This file overrides the built-in Node 0.11.x runner
// For more information see http://docs.c9.io:8080/#!/api/run-method-run
{
"cmd": [
"bash",
"--login",
"-c",
"nvm use 0.11 > /dev/null; node --harmony ${debug?--nocrankshaft --nolazy --debug-brk=15454} '$file' $args"
],
"debugger": "v8",
"debugport": 15454,
"info": "Your code is running at \\033[01;34m$url\\033[00m.\n\\033[01;31mImportant:\\033[00m use \\033[01;32mprocess.env.PORT\\033[00m as the port and \\033[01;32mprocess.env.IP\\033[00m as the host in your scripts!\n"
}

Now if go to your index.js file and press ‘Run’. This should still throw some errors. If you look at the output window, you’ll see in the upper right corner the following line: ‘Runner: Node.js (default)’, click this line and from the list select ‘Node 0.11.x’ and press run again.


You Koa.Js server should now be up and running.

No comments:

Post a Comment