A Simple Meteo App using Node.js and the API from OpenWeatherMap

NSerus
3 min readMar 20, 2021

--

Here we adapt an existing API to a little project we are going to make, nothing fancy, just to show how easy it is to export data from an API service in the form of a Weather script that gives the information that we want from the OWM (OpenWeatherMap) API.

So, in this tutorial we are using:

  • Node.js — An open-source JavaScript code interpreter that works on the server-side based on the V8 interpreter from Google. I have also made a tutorial on how to install it on Arch distributions here.
  • OpenWeatherMap — A service that provides global weather data via API. This API is public and it permits us to access any weather info from the city that we select.

To create this app we first need to…

Create an account on OpenWeatherMap

We must have a new account for us to have access to a API Key, create it here.

You must then Click your profile on the header of the page, and go to API keys, here:

For this black theme use the Dark Reader plugin on firefox

Then create a new key by giving it a Name and click on Generate, mine is “test”.

Creating a Standard App

It is advisable to make this project inside a directory (mine is nodejs-weather) for organization purposes.

We can create a standard project using the npm init command. Respond the questions as you wish:

Now it’s time to code the program, and that means getting the API info.

Getting the OWM API information

You first need to get the request module from npm. To install this module just run:

npm install request --save

WARNING: This module should only be used in certain contexts, since it’s a depreciated module.

Just use the following code and use your key to fill the “***”.

let request = require('request');

let apiKey = '*****************************';
let city = 'Guarda';
let url = `http://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${apiKey}&units=metric`

request(url, function (err, response, body) {
if(err){
console.log('error:', error);
} else {
console.log('body:', body);
}
});

Executing this code will get us the following result:

Non parsed result

Now, as you can see, this is only a raw format, it is not very user friendly to read this. Let’s change that.

Parsing

The console.log('body:', body);line is the part where we get the results.

We can parse it with:

let weather = JSON.parse(body);

Parsing this body output to a JSON file will allow us to get a much more manageable format to send and interpret through all the platforms, even the one that we are using:

let dados = `Data for ${weather.name}:
-Temps: ${weather.main.temp}ºC
- Humidity: ${weather.main.humidity}%`;
console.log(dados);

In the end, the total code of the .js file will look like this:

let request = require('request');

let apiKey = '*****************************';
let city = 'Guarda';
let url = `http://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${apiKey}&units=metric`

request(url, function (err, response, body) {
if(err){
console.log('error:', error);
} else {
let weather = JSON.parse(body);
let dados = `Data for ${weather.name}:
-Temps: ${weather.main.temp}ºC
-Humidity: ${weather.main.humidity}%`;

console.log(dados);
}
});

And give this result:

Congrats, now you have made a program that uses API data.

--

--

NSerus
NSerus

Written by NSerus

A Computer Science Student

No responses yet