Forums/Using Cloud9 IDE/How To

Create a CoffeeScript / Node.js Project

Daniela Gavidia
posted this on October 18, 2011 18:06

In this article you'll learn how to create a Hello World Coffeescript application.

Before you continue with this example, you can either create the projects and files that are shared in this example manually; or, you can clone it from GitHub using the following command:

git clone git://github.com/fjakobs/cloud9-coffeescript-example.git

To learn how to clone a GitHub repository, read this article.

A Simple CoffeeScript App in Cloud9 IDE

After cloning the project, you'll find three different files in the tree: server.js, app.coffee, and README.md.

The README.md file contains instructions to install coffee-script using the Node Package Manager. We integrated Node Package Manager into Cloud 9 IDE to enable users to install Node programs.

From the Cloud 9 IDE command line, type the following command to install the Coffeescript module:

npm install coffee-script

Next, let's have a look at the server.js file. 

require("coffee-script"); 
var app = require("./app");
app.listen(process.env.PORT);

The first line is the require() function, which is used to load the coffee-script module that you have just installed. On the second line, we declare the CoffeeScript file that contains your application. In the last line, we specify the port the server is listening to. When projects run within Cloud9 IDE, you must retrieve the port information using process.env.PORT.

Now, let's look at what the CoffeeScript file does.

It creates an HTTP server with a function that is called for each request. In the callback function, you create a response with a status code of 200 (indicating that the request was fulfilled successfully) and the message "Hello World". You use 'module.exports' to enable the server.js file to use the code in the CoffeeScript file.

http = require "http"

module.exports = http.createServer((req, res) ->
    res.writeHead 200, 'Content-Type': 'text/plain'
    res.end 'Hello World\n'
)

Next, run the server.js file and open the URL indicated in the console:

Screen_Shot_2011-11-23_at_17.54.34.png

The result is:

Screen_Shot_2011-11-23_at_17.55.05.png

To stop your application, go back to the editor and click on the stop button (next to the run button).

Congratulations! You have just written and run a CoffeeScript application in Cloud9 IDE!

 

Comments

User photo
Benekastah

Is there a way to access the `coffee` command or the `cake` command? I require coffeescript's `cake` command line utility for building my project before it runs. It would also be nice to access coffeescript's repl.

December 01, 2011 03:51
User photo
Drew Gilliam

This is probably a nube question, but how do I add another .coffee file and run it? for example I want to add another 'page' to my app called Start.coffee, how would I get it to run on the server shown here?

February 06, 2012 05:32
User photo
Martin Keefe

I don't want to write a server in node/coffeescript but C9 seems to assume that. How can I set it to run/debug command line programs written in java/coffee -script?

June 24, 2013 16:12