-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
80 lines (67 loc) · 2.78 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
// 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=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);
// return temperature;
// }
// const server=http.createServer((req,res)=>{
// if(req.url=="/"){
// requests('https://api.openweathermap.org/data/2.5/weather?q=haridwar&appid=a50dd62de35fbcfd2f42fb717ec8de17' )
// .on('data', (chunk)=> {
// const objdata=JSON.parse(chunk);
// const arrData=[objdata];
// //console.log(arrData[0].main.temp);
// const realTimeData=arrData.map((val)=> replaceval(homeFile,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");
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=haridwar&appid=a50dd62de35fbcfd2f42fb717ec8de17`
)
.on("data", (chunk) => {
const objdata = JSON.parse(chunk);
const arrData = [objdata];
// console.log(arrData[0].main.temp);
const realTimeData = arrData
.map((val) => replaceVal(homeFile, val))
.join("");
res.write(realTimeData);
// console.log(realTimeData);
})
.on("end", (err) => {
if (err) return console.log("connection closed due to errors", err);
res.end();
});
} else {
res.end("File not found");
}
});
server.listen(8000, "127.0.0.1");