Skip to content

Instantly share code, notes, and snippets.

@pdeschen
Created April 6, 2011 19:58
Show Gist options
  • Save pdeschen/906395 to your computer and use it in GitHub Desktop.
Save pdeschen/906395 to your computer and use it in GitHub Desktop.
A node.js static file server using mime.
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