Skip to content

Instantly share code, notes, and snippets.

@grantmichaels
Forked from patrickdanger/server.coffee
Created May 13, 2012 18:28
Show Gist options
  • Save grantmichaels/2689631 to your computer and use it in GitHub Desktop.
Save grantmichaels/2689631 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