Node.js Open Weather Map API for Weather Forecasts

08 Oct, 2021
The Open Weather Map API is very popular as it allows you to request weather forecasts and historical weather data programmatically.Feature of Open Weather Map API:  
It is easy to get started and easy to use.It is widely used and popular API for Weather Forecasts.
It is easy to get started and easy to use.
It is widely used and popular API for Weather Forecasts.
Installation of request module:  
You can visit the link to Install Request module. You can install this package by using this command.  npm install requestAfter installing request module you can check your request version in command prompt using the command.  npm version requestNow go to Open Weather Map website and create an account and get your API KEY.After that, you can create a folder and add a file, for example index.js. To run this file you need to run the following command.  
You can visit the link to Install Request module. You can install this package by using this command.  
npm install request
After installing request module you can check your request version in command prompt using the command.  
npm version request
Now go to Open Weather Map website and create an account and get your API KEY.
After that, you can create a folder and add a file, for example index.js. To run this file you need to run the following command.  
node index.js
Filename: index.js  
index.js
const request = require('request'); var API_KEY = 'your_api_key';   const forecast = function (latitude, longitude) {   var url = `http://api.openweathermap.org/data/2.5/weather?`            +`lat=${latitude}&lon=${longitude}&appid=${API_KEY}`      request({ url: url, json: true }, function (error, response) {         if (error) {             console.log('Unable to connect to Forecast API');         }           else {               console.log('It is currently '                + response.body.main.temp                + ' degrees out.'            );               console.log('The high today is '                + response.body.main.temp_max                 + ' with a low of '                + response.body.main.temp_min            );               console.log('Humidity today is '                + response.body.main.humidity            );         }     }) }   var latitude = 22.7196; // Indore latitude var longitude = 75.8577; // Indore longitude   // Function call forecast(latitude, longitude); 
Steps to run the program:  
The project structure will look like this:  Make sure you have installed request module using following command:  npm install requestRun index.js file using below command:  node index.js
The project structure will look like this:  
Make sure you have installed request module using following command:  
npm install request
Run index.js file using below command:  
node index.js
So this is how you can use the Open Weather Map API which allows you to request weather forecasts and historical weather data programmatically. 
mridulmanochagfg
Node.js-Misc
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Node.js fs.writeFile() Method
How to install the previous version of node.js and npm ?
Difference between promise and async await in Node.js
Mongoose | findByIdAndUpdate() Function
Installation of Node.js on Windows
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?
Differences between Functional Components and Class Components in React
Remove elements from a JavaScript Array