-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
40 lines (33 loc) · 1.45 KB
/
index.js
File metadata and controls
40 lines (33 loc) · 1.45 KB
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
38
39
40
const http = require("http");
const fs = require("fs");
var requests = require("requests");
const homeFile =fs.readFileSync("home.html","utf-8");
const replaceval=(tempval,orgval)=>{
let temperature = tempval.replace("{%tempval%}",orgval.main.temp);
temperature = temperature.replace("{%tempmin%}",orgval.main.temp_min);
temperature = temperature.replace("{%tempmax%}",orgval.main.temp_max);
temperature = temperature.replace("{%location%}",orgval.name);
temperature = temperature.replace("{%country%}",orgval.sys.country);
temperature = temperature.replace("{%tempstatus%}",orgval.weather[0].main);
return temperature;
};
const server = http.createServer((req, res) => {
if (req.url == "/") {
requests("https://api.openweathermap.org/data/2.5/weather?q=surat&appid=93992763062f743ecf494e731d98bfb5")
.on("data", (chunk) => {
const objdata=JSON.parse(chunk);
const arrdata = [objdata];
const realTimeDate = arrdata
.map((val) => replaceval(homeFile,val))
.join("")
res.write(realTimeDate)
//console.log(realTimeDate);
})
.on("end",(err) => {
if (err) return console.log("connection closed due to errors", err);
res.end();
//console.log('end');
});
}
});
server.listen(8000,"127.0.0.1");