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.
[javascript]
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/’);
[/javascript]
Converting the code to coffeescript and putting it in a file called server.coffee.
[javascript]
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/’
[/javascript]
Simple Usage
Now you can definitely compile the server.coffee to javascript and run it using the node command like this –
[bash]
$ coffee -c server.coffee
$ node server.js
Server running at http://127.0.0.1:1337/
[/bash]
Even better, you can simple execute the script by executing coffee without any options –
[bash]
$ coffee server.coffee
Server running at http://127.0.0.1:1337/
[/bash]
Executable File
You may add the following shebang to your script (and make it an executable file) to use coffee as the interpreter –
[bash]
#!/usr/bin/env coffee
# coffee code follows …
[/bash]
Then give your script executable permission and execute it.
[bash]
$ chmod +x server.coffee
$ ./server.coffee
Server running at http://127.0.0.1:1337/
[/bash]
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 –
[bash]
$ 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/
[/bash]
Are you using forever to run your servers forever? Just use the -c option to pass a command to execute.
[bash]
$ 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/
[/bash]
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).
[javascript]
require(‘coffee-script’)
require(‘./server’)
[/javascript]
Obviously for this you’ll need to have coffeescript installed.
[bash]
$ 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
[/bash]
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!