-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
83 lines (75 loc) · 2.9 KB
/
index.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// const http = require("http");
// const fs = require("fs");
// const requests = require("requests");
// const indexFile = fs.readFileSync("index.html", "utf-8");
// const replaceVal = (tempVal, orgVal) => {
// //here tempVal is htmlData and orgVal is API fetch data
// let temperature = tempVal.replace("{%tempval%}", orgVal.main.temp);
// temperature = tempVal.replace("{%tempmin%}", orgVal.main.temp_min);
// temperature = tempVal.replace("{%tempmax%}", orgVal.main.temp_max);
// temperature = tempVal.replace("{%location%}", orgVal.name);
// temperature = tempVal.replace("{%country%}", orgVal.sys.country);
// temperature = tempVal.replace("{%tempStatus%}", orgVal.weather[0]);
// // console.log(orgVal.main.temp); // checking if data is correct or not
// return temperature;
// };
// const server = http.createServer((req, res) => {
// if (req.url == "/")
// requests(
// "https://api.openweathermap.org/data/2.5/weather?q=Delhi&appid=7a1172319da083a113c44ad96da1d389",
// )
// .on("data", (chunk) => {
// const objData = JSON.parse(chunk);
// // console.log(objData);
// // const minTEmp = objData.main.temp_min;
// // const maxTemp = objData.main.temp_max;
// // const location = objData.name;
// // console.log(location, minTEmp, maxTemp);
// const arrData = [objData];
// // console.log(arrData[0].main.temp);
// const realTimeData = arrData
// .map((val) => replaceVal(indexFile, val))
// .join(" ");
// res.write(realTimeData);
// // console.log(realTimeData);
// })
// .on("end", (err) => {
// if (err) return console.log("connection closed due to errors", err);
// res.end();
// });
// });
// server.listen(8000, "127.0.0.1");
const http = require("http");
const fs = require("fs");
const requests = require("requests");
const indexFile = fs.readFileSync("index.html", "utf-8");
const replaceVal = (tempVal, orgVal) => {
let temperature = tempVal
.replace("{%tempval%}", orgVal.main.temp)
.replace("{%tempmin%}", orgVal.main.temp_min)
.replace("{%tempmax%}", orgVal.main.temp_max)
.replace("{%location%}", orgVal.name)
.replace("{%country%}", orgVal.sys.country)
.replace("{%tempStatus%}", orgVal.weather[0]);
return temperature;
// console.log(orgval.main.temp);
};
const server = http.createServer((req, res) => {
if (req.url == "/")
requests(
"https://api.openweathermap.org/data/2.5/weather?q=Delhi&units=metric&appid=7a1172319da083a113c44ad96da1d389",
)
.on("data", (chunk) => {
const objData = JSON.parse(chunk);
const arrData = [objData];
const realTimeData = arrData
.map((val) => replaceVal(indexFile, val))
.join(" ");
res.write(realTimeData);
})
.on("end", (err) => {
if (err) return console.log("connection closed due to errors", err);
res.end();
});
});
server.listen(8000, "127.0.0.1");