Skip to content

Instantly share code, notes, and snippets.

@mhoyer
Last active August 29, 2015 14:17
Show Gist options
  • Save mhoyer/108f939fb27e5631eacf to your computer and use it in GitHub Desktop.
Save mhoyer/108f939fb27e5631eacf to your computer and use it in GitHub Desktop.
Tiny, simple HTTP server
// run with `node httpserve.js` and go to shown url
var http = require('http'),
path = require('path'),
fs = require('fs');
var webroot = __dirname,
port = 8080;
http.createServer(function(req, res) {
var url = req.url;
console.log(req.method + ' ' + url);
if (url == undefined || url == '' || url == '/') url = 'index.html';
fs.readFile(path.join(webroot, url), function(err, data) {
res.writeHead(200);
res.end(data);
});
}).listen(port);
console.log('HTTP at http://localhost:' + port);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment