Skip to content

Instantly share code, notes, and snippets.

@jpalala
Created February 3, 2015 10:27
Show Gist options
  • Save jpalala/5e65493fbf62275214f9 to your computer and use it in GitHub Desktop.
Save jpalala/5e65493fbf62275214f9 to your computer and use it in GitHub Desktop.
simple webserver in nodejs
var http = require('http')
, url = require('url')
, fs = require('fs')
, server;
server = http.createServer(function(req, res) {
var path = url.parse(req.url).pathname;
switch(path) {
case '/test':
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('itworks\n');
res.end()
break;
//server up an html chat client file
case '/':
fs.readFile(__dirname + '/index.html', function(err, data) {
if(err) return send404(res);
res.writeHead(200, {'Content-Type': 'text/html'})
res.write(data, 'utf8');
res.end();
});
break;
default: send404(res);
}
});
send404 = function(res) {
res.writeHead(404);
res.write('404');
res.end();
};
server.listen(8081);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment