How to create weather app in nodeJS
Create server and Fetch API
npm requests module usageconst http = require('http');var requests = require('requests');const server = http.createServer((req, res)=>{console.log("server is running");if(req.url == '/'){requests("https://api.openweathermap.org/data/2.5/weather?q=Ahmednagar&appid=28e1107da44a4e4406402748f062d353").on("data",(chunkData)=>{console.log(chunkData);})// .on("end",(err)=>{// console.log("connection fail");// })}});server.listen(8000,'127.0.0.1',()=>{console.log('listening port no');});
asdssarequests('https://google.com/foo/bar', { streaming }).on('data', function (chunk) {console.log(chunk)}).on('end', function (err) {if (err) return console.log('connection closed due to errors', err);console.log('end');});
const fs = require('fs');const http = require('http');var requests = require('requests');const weatherFile = fs.readFileSync("weather.html","utf-8");const replaceVal=(tempVal, orgVal)=>{let temp = tempVal.replace("{%tempval%}",orgVal.main.temp);temp = temp.replace("{%tempmin%}",orgVal.main.temp_min);temp = temp.replace("{%tempmax%}",orgVal.main.temp_max);temp = temp.replace("{%location%}",orgVal.name);temp = temp.replace("{%country%}",orgVal.sys.country);return temp;}const server = http.createServer((req, res)=>{// console.log("server is running");if(req.url == '/'){requests("https://api.openweathermap.org/data/2.5/weather?q=Ahmednagar&appid=28e1107da44a4e4406402748f062d353").on("data",(chunkData)=>{// JSON data convert to javascript objectconst objData = JSON.parse(chunkData);// javascript object data convert to arrayconst arrData = [objData];// console.log(arrData);// using map method access all valuesconst realTimeData = arrData.map((val)=>replaceVal(weatherFile,val)).join("");// console.log(realTimeData);// read data in webpageres.write(realTimeData);}).on("end",(err)=>{if (err) return console.log("connection fail due to :",err);// console.log('end');//no more data to readres.end();});}});server.listen(8000,'127.0.0.1');
Post a Comment