Created
January 22, 2013 11:34
-
-
Save nickleefly/4593953 to your computer and use it in GitHub Desktop.
server static file
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 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