Skip to content

Instantly share code, notes, and snippets.

@hughrawlinson
Last active February 11, 2016 11:46
Show Gist options
  • Save hughrawlinson/5524836 to your computer and use it in GitHub Desktop.
Save hughrawlinson/5524836 to your computer and use it in GitHub Desktop.
A Node server for showing temperature from a sensor on a Raspberry Pi
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('<html><body><h1>Welcome to W1-TempServ</h1>\n');
var temp = '';
fs.readFile('/home/simon/temp', 'utf8', function (err, data) {
if (err) throw err;
console.log(data);
res.write("<p>"+data+"</p></body></html>");
res.end();
});
// Data will not work here because it's outside of the it's scope.
// it was defined in the fs.readFile callback, can't be accessed outside of that
// res.write(data);
//also because of js asynchronicity you'll need to write everything in the callback
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment