-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDemo.js
37 lines (29 loc) · 1.23 KB
/
Demo.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
30
31
32
33
34
35
36
37
const http = require("http");
const fs = require("fs");
var requests = require("requests");
const homeFile = fs.readFileSync("./index.html", "utf8");
const replaceVal = (tempVal, orgval) => {
let temmperature = tempVal.replace("{%tempval%}", orgval.main.temp);
temmperature = temmperature.replace("{%tempmin%}", orgval.main.temp_min);
temmperature = temmperature.replace("{%tempmax%}", orgval.main. temp_max);
temmperature = temmperature.replace("{%location%}", orgval.name);
temmperature = temmperature.replace("{%country%}", orgval.sys.country);
temmperature = temmperature.replace("{%tempstatus%}", orgval.weather[0].main);
return temmperature;
}
const server = http.createServer((req,res)=>{
if(req.url == "/"){
requests('https://api.openweathermap.org/data/2.5/weather?q=delhi&units=metric&appid=11d0e7c78d84d0afd47a7c42a81bbb9e')
.on('data', (chunk) => {
const objdata = JSON.parse(chunk)
const arrData = [objdata];
const realTimeData = arrData.map((val) => replaceVal(homeFile,val)).join("");
res.write(realTimeData);
})
.on('end', (err) => {
if (err) return console.log('connection closed due to errors', err);
res.end();
});
}
});
server.listen(4000, "127.0.0.1");