Skip to content

Instantly share code, notes, and snippets.

@nickleefly
Created January 22, 2013 11:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nickleefly/4593953 to your computer and use it in GitHub Desktop.
Save nickleefly/4593953 to your computer and use it in GitHub Desktop.
server static file
var http = require('http');
var path = require('path');
var fs = require('fs');
http.createServer(function(request, response) {
var lookup = path.basename(decodeURL(request.url)) || 'index.html';
f = 'content/' + lookup;
fs.exists(f, function(exists) {
if(exists) {
fs.readFile(f, function(err, data) {
if(err) {
response.writeHead(500);
response.end('Server Error!');
return;
}
var headers = {'Content-Type': mimeTypes[path.extname(lookup)]};
response.writeHead(200, headers);
response.end(data);
});
return;
}
response.writeHead(400);
response.end();
});
}).listen(8080);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment