Created
April 23, 2011 04:00
-
-
Save patrickdanger/938253 to your computer and use it in GitHub Desktop.
Basic FileServer in node.js
This file contains hidden or 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
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