Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added weather fetching api #260

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Existing_API_Collection/Weather_API/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.env
89 changes: 89 additions & 0 deletions Existing_API_Collection/Weather_API/app.js
Original file line number Diff line number Diff line change
@@ -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("<h1>The temperature in " + query + " is " + temp + " degrees.</h1>");
// res.write("<p>" + descp + "</p>");
// res.write("<img src=" + imgurl + ">");
// 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(`<h1>The temperature in ${query} is ${temp} degrees.</h1>`);
res.write(`<p>${descp}</p>`);
res.write(`<img src="${imgurl}">`);
res.send();
});
});
});

app.listen(3003, function () {
console.log("server running");
});
20 changes: 20 additions & 0 deletions Existing_API_Collection/Weather_API/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Weather App</title>
</head>

<body>
<form action="/" method="post">
<label for="">City Name</label>
<input id="cityInput" type="text" name="cityName">
<button type="submit">Go</button>
</body>
</form>
</body>

</html>
Loading
Loading