Skip to content

Instantly share code, notes, and snippets.

@tiye
Created June 20, 2012 13:38
Show Gist options
  • Save tiye/2959922 to your computer and use it in GitHub Desktop.
Save tiye/2959922 to your computer and use it in GitHub Desktop.
serve static pages up as if it's on server
#!/usr/bin/coffee
fs = require 'fs'
http = require 'http'
mime = require 'mime'
path = require 'path'
here = process.env.PWD
index = process.argv[2]
port =
if process.argv[3]? then Number process.argv[3]
else 8001
http.createServer (req, res) ->
aim = req.url
if aim is '/' then aim = index
aim = path.join here, aim
console.log aim
if (path.existsSync aim)
fs.readFile aim, (err, file) ->
throw err if err?
res.writeHead 200, 'Content-Type': (mime.lookup aim)
res.end file
else res.end ''
.listen port
console.log 'listen port:', port
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment