forked from webofthings/wot-book
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlisting-3.2-webserver.js
30 lines (26 loc) · 939 Bytes
/
listing-3.2-webserver.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
var http = require("http");
var port = 8686;
function randomInt (low, high) {
return Math.floor(Math.random() * (high - low) + low);
}
http.createServer(function(req,res){
console.log('New incoming client request for ' + req.url);
res.writeHeader(200, {'Content-Type': 'application/json'}); //#A
switch(req.url) { //#B
case '/temperature':
// And return the corresponding JSON
res.write('{"temperature" :' + randomInt(1, 40) + '}'); //#C
break;
case '/light':
res.write('{"light" :' + randomInt(1, 100) + '}');
break;
default:
res.write('{"hello" : "world"}');
}
res.end(); //#D
}).listen(port);
console.log('Server listening on http://localhost:' + port);
//#A Setting the header to announce we return JSON representations
//#B Read the request URL and provide responses accordingly
//#C Write the temperature result as JSON
//#D Causes to return the results to the client