Created
February 15, 2010 01:45
-
-
Save hugs/304374 to your computer and use it in GitHub Desktop.
The Node.js "Hello World" web server ported to CoffeeScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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/" |
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/'
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
Doesn't seem to be working with CoffeeScript 1.0.1.
Should be: