Skip to content

Instantly share code, notes, and snippets.

@anthonywebb
Created August 24, 2013 01:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anthonywebb/6325443 to your computer and use it in GitHub Desktop.
Save anthonywebb/6325443 to your computer and use it in GitHub Desktop.
Basic node HTTP Server
var http = require('http');
var url = require('url');
var host = '127.0.0.1';
var port = 8080;
var zones = ["P9_11","P9_13","P9_15","P9_17","P9_21","P9_23","P9_25","P9_27","P9_28","P9_26","P9_24","P9_22","P9_18","P9_16","P9_14","P9_12"];
var rain = 'P9_29';
var button = 'P9_30';
http.createServer(function (req, res) {
var q = url.parse(req.url,true);
console.log(q);
if(q.pathname == '/zones'){
sendJSON(res, zones);
} else {
// everything else gets a 404 not found
throw404(res);
}
}).listen(port, host);
function sendJSON(response, data){
response.writeHead(200, {'Content-Type': 'application/json'});
response.write(JSON.stringify(data));
response.end();
return;
}
function throw404(response){
response.writeHead(404, {'Content-Type': 'text/plain'});
response.write("404 Not Found\n");
response.end();
return;
}
console.log('Server running at '+host+':'+port);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment