Using CoffeeScript over JavaScript in your Node.js Application or Module

If you’ve wanted to code your next Node.js app in CoffeeScript then it ain’t that hard. Since coffeescript is just a little language that compiles down to javascript it is pretty much compatible with node.js. Let’s take a look at some of the ways in which you can execute your node.js script (written in coffeescript).

We’ll reuse the simple web server code found on nodejs.org.

var http = require('http');

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(1337, '127.0.0.1');

console.log('Server running at http://127.0.0.1:1337/');

Converting the code to coffeescript and putting it in a file called server.coffee.

http = require 'http'

http.createServer (req, res) ->
  res.writeHead 200, {'Content-Type': 'text/plain'}
  res.end 'Hello World\n'
.listen 1337, '127.0.0.1'

console.log 'Server running at http://127.0.0.1:1337/'

Simple Usage

Now you can definitely compile the server.coffee to javascript and run it using the node command like this -

$ coffee -c server.coffee
$ node server.js
Server running at http://127.0.0.1:1337/

Even better, you can simple execute the script by executing coffee without any options -

$ coffee server.coffee 
Server running at http://127.0.0.1:1337/

Executable File

You may add the following shebang to your script (and make it an executable file) to use coffee as the interpreter -

#!/usr/bin/env coffee

# coffee code follows ...

Then give your script executable permission and execute it.

$ chmod +x server.coffee
$ ./server.coffee 
Server running at http://127.0.0.1:1337/

This approach of making your script an executable file is not something I usually pick.

Using nodemon or forever

If you’re using nodemon to watch files for changes and restart node server in your development then it works pretty well with coffee implicitly -

$ nodemon server.coffee
14 Mar 01:24:32 - [nodemon] v0.7.2
14 Mar 01:24:32 - [nodemon] watching: /var/www/dev/node
14 Mar 01:24:32 - [nodemon] starting `coffee server.coffee`
Server running at http://127.0.0.1:1337/

Are you using forever to run your servers forever? Just use the -c option to pass a command to execute.

$ forever start -c coffee server.coffee 
info:    Forever processing file: server.coffee
$ forever list
info:    Forever processes running
data:        uid  command script        forever pid   logfile                          uptime      
data:    [0] iv2r coffee  server.coffee 16710   16712 /home/codetheory/.forever/iv2r.log 0:0:0:2.508 
$ cat ~/.forever/iv2r.log 
Server running at http://127.0.0.1:1337/

via JavaScript Code

There’s one more way, you can have a index.js file that loads your server.coffee (that in turn loads your entire web application).

require('coffee-script')
require('./server')

Obviously for this you’ll need to have coffeescript installed.

$ npm install coffee-script
npm http GET https://registry.npmjs.org/coffee-script
npm http 304 https://registry.npmjs.org/coffee-script
coffee-script@1.6.1 node_modules/coffee-script

This method is helpful when your’re writing a node module in coffeescript that other people can install via npm and re-use no matter what they use for their own codebase (javascript or coffeescript).

Hope these tips help!

Share:

One thought on “Using CoffeeScript over JavaScript in your Node.js Application or Module

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>