Created
April 6, 2011 19:58
-
-
Save pdeschen/906395 to your computer and use it in GitHub Desktop.
A node.js static file server using mime.
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
var libpath = require('path'), | |
http = require("http"), | |
fs = require('fs'), | |
url = require("url"), | |
mime = require('mime'); | |
var path = "."; | |
var port = 8088; | |
http.createServer(function (request, response) { | |
var uri = url.parse(request.url).pathname; | |
var filename = libpath.join(path, uri); | |
libpath.exists(filename, function (exists) { | |
if (!exists) { | |
response.writeHead(404, { | |
"Content-Type": "text/plain" | |
}); | |
response.write("404 Not Found\n"); | |
response.end(); | |
return; | |
} | |
if (fs.statSync(filename).isDirectory()) { | |
filename += '/index.html'; | |
} | |
fs.readFile(filename, "binary", function (err, file) { | |
if (err) { | |
response.writeHead(500, { | |
"Content-Type": "text/plain" | |
}); | |
response.write(err + "\n"); | |
response.end(); | |
return; | |
} | |
var type = mime.lookup(filename); | |
response.writeHead(200, { | |
"Content-Type": type | |
}); | |
response.write(file, "binary"); | |
response.end(); | |
}); | |
}); | |
}).listen(port); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment