Skip to content

Instantly share code, notes, and snippets.

@hbbio
Created October 13, 2012 18:38
Show Gist options
  • Save hbbio/3885701 to your computer and use it in GitHub Desktop.
Save hbbio/3885701 to your computer and use it in GitHub Desktop.
function server() {
xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET","http://localhost:8001/getstring", true);
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
string=xmlhttp.responseText;
}
}
xmlhttp.send();
}
var http = require('http'),
fs = require('fs'),
url = require('url'),
choices = ["hello world", "goodbye world"];
http.createServer(function(request, response) {
var path = url.parse(request.url).pathname;
if(path=="/getstring"){
var string = choices[Math.floor(Math.random()*choices.length)];
response.writeHead(200, {"Content-Type": "text/plain"});
response.end(string);
}else{
fs.readFile('./index.html', function(err, file) {
if(err) {
return;
}
response.writeHead(200, { 'Content-Type': 'text/html' });
response.end(file, "utf-8");
});
}
}).listen(8001);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment