Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Jalalhejazi/5473996 to your computer and use it in GitHub Desktop.
Save Jalalhejazi/5473996 to your computer and use it in GitHub Desktop.
node: basic static http server.
var http = require('http');
var url = require('url');
var mime = require('mime');
var fs = require('fs');
var port = process.env.PORT || 40000;
http.createServer(function(req, res) {
var u = url.parse(req.url);
var filename = __dirname + '/static' + u.pathname;
fs.exists(filename, function(exists) {
if (!exists) {
res.writeHead(404, {'Content-Type': 'text/plain'});
return res.end();
}
res.setHeader("Content-Type", mime.lookup(filename));
fs.createReadStream(filename).pipe(res);
});
}).listen(port);
console.log('listening on port:', port);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment