Skip to content

Instantly share code, notes, and snippets.

@flesch
Created August 18, 2012 15:19
Show Gist options
  • Save flesch/3387538 to your computer and use it in GitHub Desktop.
Save flesch/3387538 to your computer and use it in GitHub Desktop.
Simple node.js static file server.
http = require "http"
url = require "url"
path = require "path"
fs = require "fs"
http.createServer (request, response) ->
file = path.join process.cwd(), url.parse(request.url).pathname
fs.exists file, (exists) ->
unless exists
response.writeHead 404, "Content-Type":"text/plain"
response.write "404 Not Found"
response.end "\n"
return
file += "/index.html" if fs.statSync(file).isDirectory()
fs.readFile file, "binary", (err, file) ->
if err
response.writeHead 500, "Content-Type":"text/plain"
response.write err
response.end "\n"
return
response.writeHead 200
response.write file, "binary"
response.end()
return
.listen process.env.PORT or 5000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment