-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackEnd.js
38 lines (32 loc) · 1.34 KB
/
backEnd.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
let http = require('http');
var requests = require('requests');
const fs = require('fs');
// getting data at backend using Nodejs
const homeFile = fs.readFileSync('home.html',"utf-8");
const replaceVal =(tempVal, origVal)=>{
let temperature = tempVal.replace('{%tempval%}', origVal.main.temp);
temperature= temperature.replace("{%tempmin%}", origVal.main.temp_min);
temperature= temperature.replace("{%tempmax%}", origVal.main.temp_max);
temperature= temperature.replace("{%location%}", origVal.name);
temperature= temperature.replace("{%country%}", origVal.sys.country);
return temperature;
}
// console.log(homeFile);
const server = http.createServer((req, res)=>{
if(req.url == '/'){
requests("http://api.openweathermap.org/data/2.5/weather?q=Raipur&appid=6781f1e3836eda9f9ad56a8c83f75302")
.on('data', (chunk)=> {
let objData = JSON.parse(chunk);
const arrData = [objData];
// console.log(arrData[0].main.temp);
const realTimeData = arrData.map(val => replaceVal(homeFile, val)).join("");
console.log(realTimeData);
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');