Created
February 3, 2015 10:27
-
-
Save jpalala/5e65493fbf62275214f9 to your computer and use it in GitHub Desktop.
simple webserver in nodejs
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') | |
, 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