diff --git a/Existing_API_Collection/Weather_API/.gitignore b/Existing_API_Collection/Weather_API/.gitignore new file mode 100644 index 0000000..4c49bd7 --- /dev/null +++ b/Existing_API_Collection/Weather_API/.gitignore @@ -0,0 +1 @@ +.env diff --git a/Existing_API_Collection/Weather_API/app.js b/Existing_API_Collection/Weather_API/app.js new file mode 100644 index 0000000..f6d5f02 --- /dev/null +++ b/Existing_API_Collection/Weather_API/app.js @@ -0,0 +1,89 @@ +// const express = require("express"); //server nai chlega,get, post method vgera isme +// const https = require("https"); //server se data request, data transfer with security +// const bodyParser = require("body-parser"); //read data from requested server +// const app = express(); //function call +// app.use(bodyParser.urlencoded({ extended: true })); +// app.get("/", function (req, res) { //root=homepage homepage pr file send krega index.html +// res.sendFile(__dirname + "/index.html"); + + +// }); +// app.post("/", function (req, res) { //form me method=post h,form filled to fn(req,res) performed + +// const query = req.body.cityName; // take cityname from the form + +// const url = "https://api.openweathermap.org/data/2.5/weather?q=" + query + "&appid=665982ba9621921bae463c48f3430c48&units=metric"; +// https.get(url, function (response) { //another server se url milega to function(response pr jaega) +// console.log(response.statusCode); //logs status code + +// response.on("data", function (data) { //"data" is event listener,emiited when new data is available to read. +// const weatherData = JSON.parse(data); //convert data from string to js obj + +// const temp = weatherData.main.temp; +// const icon = weatherData.weather[0].icon; +// const imgurl = " https://openweathermap.org/img/wn/" + icon + "@2x.png"; + +// const descp = weatherData.weather[0].description; +// res.write("
" + descp + "
"); +// res.write(""); +// res.send(); //one res.send hona chhiye onliii +// }); +// }); +// } +// ) + + + + + + + + + +// app.listen(3003, function () { +// console.log("server running"); +// }); +const express = require("express"); +const https = require("https"); +const bodyParser = require("body-parser"); +const dotenv = require("dotenv"); + +// Load environment variables from .env file +dotenv.config(); + +const app = express(); +app.use(bodyParser.urlencoded({ extended: true })); + +app.get("/", function (req, res) { + res.sendFile(__dirname + "/index.html"); +}); + +app.post("/", function (req, res) { + const query = req.body.cityName; + + // Use the API key from the environment variables + const apiKey = process.env.API_KEY; + const url = `https://api.openweathermap.org/data/2.5/weather?q=${query}&appid=${apiKey}&units=metric`; + + https.get(url, function (response) { + console.log(response.statusCode); + + response.on("data", function (data) { + const weatherData = JSON.parse(data); + const temp = weatherData.main.temp; + const icon = weatherData.weather[0].icon; + const imgurl = `https://openweathermap.org/img/wn/${icon}@2x.png`; + const descp = weatherData.weather[0].description; + + res.write(`${descp}
`); + res.write(``); + res.send(); + }); + }); +}); + +app.listen(3003, function () { + console.log("server running"); +}); diff --git a/Existing_API_Collection/Weather_API/index.html b/Existing_API_Collection/Weather_API/index.html new file mode 100644 index 0000000..d9864be --- /dev/null +++ b/Existing_API_Collection/Weather_API/index.html @@ -0,0 +1,20 @@ + + + + + + + +