Creating a weather app using node.js and the OpenWeatherMap API
Hi people!!
Today I will teach you how to make this simple app using node.js and the OpenWeatherMap API.
1st step: Create an account on the OpenWeatherMap website.
In order to make things easier, you can create the account through this link: https://home.openweathermap.org/users/sign_up
2nd step: Select the “API Keys” option, then enter a Name in the “Name” field and, finally, click on the “Generate” button;
3rd step: Open the command line and enter the following commands:
- mkdir nodejs-weather
- npm init
The result will be something like this:
4th step: Create the index.js file on the command line and insert the following commands:
- touch index.js
- npm install request --save
The result will be according to this image:
5th step: Open the index.js file and insert the following code:
let request = require('request');let apiKey = '34ef108283d61ecbca72455a49a8d6b2';let cidade = 'Guarda';let url = `http://api.openweathermap.org/data/2.5/weather?q=${cidade}&appid=${apiKey}&units=metric`request(url, function (err, response, body) {if(err){console.log('error:', error);} else {let weather = JSON.parse(body);let dados = `DADOS METEROLÓGICO PARA a ${weather.name}:------------- Temperatura (ºC): ${weather.main.feels_like}- Humidade: ${weather.main.humidity}- Visibilidade: ${weather.visibility}- Vento (km/h): ${weather.wind.speed}`console.log(dados);}});
6th step: Run the app using the command:
node index.js
And this is going to be the end result:
I hope you enjoyed!!