Skip to content

Instantly share code, notes, and snippets.

@hugs
Created February 15, 2010 01:45
  • Star 13 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save hugs/304374 to your computer and use it in GitHub Desktop.
The Node.js "Hello World" web server ported to CoffeeScript
I, Jason Huggins, the author of the work "CoffeeScript Web Server" (2010), irrevocably renounce
all current and future legal rights to the work in any medium whatsoever.
I stand behind the merit of the work, but disclaim all liability for it under law.
I encourage you, the audience, to share, copy, distribute, perform, remix, mash up, interpret,
excerpt, translate, and otherwise enjoy and use the work as you will.
I request that you acknowledge my authorship.
Digital versions of the work may be available at https://gist.github.com/gists/304374. Learn more
at pleasepirate.org.
# Install Node and CoffeeScript:
# Node : http://github.com/ry/node
# CoffeeScript : http://github.com/jashkenas/coffee-script
#
# In a terminal window:
# $ cd coffee-script
# $ ./bin/node_coffee -r web_server.coffee
#
# Tested with Mac OS X 10.5.8, Node 0.1.26, CoffeeScript 0.5.0
#
# Jeremy Ashkenas has included this script in CoffeeScript's examples directory:
# http://github.com/jashkenas/coffee-script/blob/master/examples/web_server.coffee
http: require "http"
http.createServer( (req, res) ->
res.sendHeader 200, {"Content-Type": "text/plain"}
res.sendBody "Hello, World!"
res.finish()
).listen 8000
puts "Server running at http://127.0.0.1:8000/"
@cjoudrey
Copy link

Doesn't seem to be working with CoffeeScript 1.0.1.

Should be:

sys = require "sys"
http = require "http"

@edhiley
Copy link

edhiley commented Mar 25, 2011

this works for me in cs 1.0.1

http = require "http"

http.createServer( (req, res) ->
  res.writeHead 200, {"Content-Type": "text/plain"}
  res.end "Hello, World!"
).listen 8000

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

@NimbyDagda
Copy link

If you are using coffee to be efficient, you might as well get rid of the last extraneous set of brackets and braces

http = require 'http'
http.createServer (req, res) ->
  res.writeHead 200, 'Content-Type': 'text/plain'
  res.end 'Hello, World!'
.listen 8000
console.log 'Server running at http://127.0.0.1:8000/'

and if you wanted to pass more options in the header array it would look like this

http = require 'http'
http.createServer (req, res) ->
  res.writeHead 200,
    'Content-Type': 'text/plain'
    'Content-Length': 12
  res.end 'Hello, World!'
.listen 8000
console.log 'Server running at http://127.0.0.1:8000/'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment