Weather Development Tutorials, Guides & Insights
Unlock 1+ expert-curated weather tutorials, real-world code snippets, and modern dev strategies. From fundamentals to advanced topics, boost your weather skills on DeveloperBreeze.
Adblocker Detected
It looks like you're using an adblocker. Our website relies on ads to keep running. Please consider disabling your adblocker to support us and access the content.
Code
javascript
Weather App with Node.js
const axios = require('axios');
// Replace with your own OpenWeatherMap API key
const apiKey = 'YOUR_API_KEY';
// Function to get the weather for a city
async function getWeather(city) {
try {
const response = await axios.get(`https://api.openweathermap.org/data/2.5/weather`, {
params: {
q: city,
appid: apiKey,
units: 'metric'
}
});
const weather = response.data;
const temperature = weather.main.temp;
const description = weather.weather[0].description;
const humidity = weather.main.humidity;
console.log(`Weather in ${city}:`);
console.log(`Temperature: ${temperature}°C`);
console.log(`Description: ${description}`);
console.log(`Humidity: ${humidity}%`);
} catch (error) {
console.error(`Could not retrieve weather data: ${error}`);
}
}
// Prompt the user for a city name
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
readline.question('Enter the city name: ', city => {
getWeather(city);
readline.close();
});To run the script, execute the following command in your terminal:
Aug 08, 2024
Read More