Skip to content

Instantly share code, notes, and snippets.

@patrickdanger
Created April 23, 2011 04:00
Show Gist options
  • Save patrickdanger/938253 to your computer and use it in GitHub Desktop.
Save patrickdanger/938253 to your computer and use it in GitHub Desktop.
Basic FileServer in node.js
http = require 'http'
url = require 'url'
fs = require 'fs'
path = require 'path'
server = http.createServer (req, resp) ->
uri = url.parse req.url, yes
query = uri.queryString
filepath = path.normalize uri.pathname
file = factory.file filepath
path.exists file.filepath, (exists) ->
if exists
console.log "serving file #{file}"
file.render (data) ->
resp.writeHead 200, if file.type then 'Content-Type': file.type
resp.write data
resp.end()
else
console.log "#{file} not found"
resp.writeHead 404
resp.end "404 File Not Found"
.listen 8080
console.log "Server started, listening on port 8080"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment