Created
August 18, 2012 15:19
-
-
Save flesch/3387538 to your computer and use it in GitHub Desktop.
Simple node.js static file server.
This file contains 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" | |
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