-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
45 lines (38 loc) · 1.41 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
require('dotenv').config();
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(
`http://api.openweathermap.org/data/2.5/weather?q=Pune&units=metric&appid=${process.env.APPID}`
)
.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(5000, "127.0.0.1");