-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
36 lines (32 loc) · 1.35 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
const http = require('http');
const fs = require('fs');
const port = process.env.port || 3000;
var requests = require('requests');
const homeData = fs.readFileSync('home.html', 'utf-8');
const replaceVal = (tempVal, orgVal) => {
let temperature = tempVal.replace("{%tempval%}", orgVal.main.temp);
temperature = temperature.replace("{%location%}", orgVal.name);
temperature = temperature.replace("{%mintemp%}", orgVal.main.temp_min);
temperature = temperature.replace("{%maxtemp%}", orgVal.main.temp_max);
temperature = temperature.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=Agra&appid=4f618218fb946f13627606b4649eef9d')
.on('data', (chunk) => {
const objdata = JSON.parse(chunk);
const arrData = [objdata];
var reaTimeData = arrData.map( val => replaceVal ( homeData, val )).join("");
console.log(reaTimeData);
res.write(reaTimeData);
})
.on('end', (err) => {
if (err) return console.error('connection failed', err);
res.end();
})
}
});
server.listen(port, '127.0.0.1', () => {
console.log('listening to port 3000');
});