O'Reilly logo

CoffeeScript Programming with jQuery, Rails, and Node.js by Michael Erasmus

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

"Hello World" in Node

Let's write the simplest Node app we can using CoffeeScript. Create a file named hello.coffee and enter the following code in it:

http = require('http')

server = http.createServer (req, res) ->
  res.writeHead 200
  res.end 'Hello World'

server.listen 8080

This uses the http module of Node.js, which provides capabilities for building an HTTP server. The require('http') function will return an instance of the http module, which exports a createServer function. This function takes a requestListener argument, which is a function that will respond to client requests. In this case, we respond with an HTTP status code 200 and end the response with Hello World as the request body. Finally, we call the listen method on the returned server ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required