Abrupt weather and climate change are things that everybody is dealing with. In fact, the vast majority of the global population relies on accurate, real-time weather data and forecasts to make informed decisions.

This has increased the importance of reliable Android and iOS weather apps. In this article, we will show you how to create a simple forecasting tool using NodeJS and a weather API.

But before that, let’s go over the importance of weather apps.

Why Do We Need Weather Apps?

A feature-rich weather forecasting app can provide great value to various industries. Some noteworthy benefits of weather applications include:

  • Providing immediate access to local weather conditions and upcoming forecast, thus saving you time.
  • Providing real-time notifications about prevailing and expected weather conditions.
  • Helping governments and local administrations prepare for natural disasters and save lives.
  • Helping farmers take preventive measures.
  • Facilitating the global travel and tourism industry.
  • Providing clear weather forecasts which are crucial in the aviation and logistics industry.

What You Need to Build a Weather App


Here are some of the things you will need to build a weather app successfully:

  • Familiarity working with JavaScript (Node.js)
  • A text editor such as Notepad or an IDE. My favorite is Visual Studio.
  • Access to a reliable weather API such as ClimaCell
  • Access to a map service
  • Knowledge of HTML, CSS, and Bootstrap

Once you have these ready, you’re good to go.

Overview of the ClimaCell Weather API

ClimaCell is a popular weather provider that offers hyper-accurate historical weather data as well as forecasts through an easy to consume API.

The Building Process

In this section, I will show you how I created a forecasting app where a user enters their city or any other location by name and fetches weather data from the ClimaCell API. The API responds to the request by returning data, which is then displayed to the user.

Install NodeJS and Create a New Project


For this project, we will use Node.js — one of the most popular run-time environments for JavaScript. Node.js helps developers create quick web applications. It has a wide range of libraries and modules for creating advanced web applications.

If you do not have Node.js on your device, you can install it from the official website.
Once installed, we use this command to initialize npm - the default packet manager used by Node.js.

$ npm init

This creates our project, so you will be prompted to enter a few details such as package name, description, Git repository, and more.

Next, we install the modules required to run our project. To generate a Node.js app skeleton, we use express - a framework for building Node.js web applications.

$ npm install express

Installing the express framework helps you run the server, handle client requests, and connect the right HTML template with a response.  

Next, we will also install unirest - a simple yet powerful solution that allows you to request a library.

It will help us make requests to the ClimaCell API and handle the responses.

Use this command:

npm install unirest

At this point, we’ve installed the necessary modules, and the project is ready.
Next, we generate a weather app using the express generator tool. On the command line, type this:

express --view=pug weather-app-nodejs

You should now have a view like this on the command line:

Get the ClimaCell Weather API

To get access to the ClimaCell API, you will need to sign up for an account on their page.

Once you create an account, sign in to their Microweather API dashboard which looks like this:


On the dashboard, click on references to check the API endpoints. As you can see, the ClimaCell API has a number of endpoints including the short term forecast, hourly forecast, real time data, and more.

Worth a mention is that each endpoint has its own code snippet. For example, here is the Node.js code snippet for getting real time weather data.

Raw

Modifying the application

To call the ClimaCell API, we need to first edit some files. Here, you can use Notepad or open the project directory in your IDE for easier editing. It should appear as shown:

We start modifying our files by adding bootstrap to layout.pug. Open the views directory and insert this snippet to the file.

Raw

Next, we create a form by adding the snippet below to index.pug file.

Notice how we use the HTTP post method to send data to the server. The code above also sets the action parameter to weather route and adds the text input as “city.”  
An input button to fetch the weather is also added.

We now create an HTML table just below the form to display fetched weather records.

Raw

Inserting the code snippet above creates a table that looks like this:

Calling the ClimaCell API

To send requests to the ClimaCell API, we must install the request module.

npm i request --save

Next, we add the ClimaCell API credentials in the index.js file. Open the file in your routes directory and add the API key you obtained on the ClimaCell dashboard:


Here is the code to add API credentials:

Raw


After adding the API credentials, we update the index route. This is done by replacing the code section in ‘/’ route in index.js file.

Raw

We finish by creating the weather route in index.js.

Raw


This code snippet enables data in the input form to be posted to the index route. Once the user enters a city name, it is assigned to the city variable using the request object.

The URL is then appended with the city name and ID and the request sent to ClimaCell API.

The ClimaCell API server response is returned as a JSON file, which is then parsed and fed to the output template.

For instance, if the user was looking for Boston weather forecasts, the app will return this:

Note - The temperature in this example is shown in Kelvin and is equal to 50°F or 10°C.

Add Maps to Visualize Your Data

You can integrate interactive maps into your forecasting application to enhance user experience. This can be achieved by using a third-party map service provider for web applications.

Mapbox is one such tool that helps developers create awesome weather maps for their applications. It integrates seamlessly with any weather app.

To use the Mapbox, sign up on their website and check out their API. There are integrations for Android, iOS, Web, and Unity. In this case, we choose Web integration for our tool.

We can either install the Mapbox CDN or use the module bundler. Let's use the module bundler.

The first step would be installing the package

npm install Mapbox-gl –save

Next, we add the GL JS CSS file in the HTML file by including this snippet in the <head>

<link href='https://api.mapbox.com/mapbox-gl-js/v1.8.1/mapbox-gl.css' rel='stylesheet' />

We can now add the map to our application. To do this, use the code snippet below.

Raw


You can choose where to place the map by replacing the

“CONTAINER_ELEMENT_ID’ .

Here is a sample map generated using Mapbox:

What’s next?

At this point, much of the work is done, and your app can get weather forecasts for any city using the ClimaCell API.

However, you can consider adding more interactive features to your application or extending its functionality.

Here are some things you might want to do:

  • Add a search function.
  • Improve the look of your user interface.
  • Query the application by ID or name.
  • Display a list of target cities and their respective IDs.
  • Add parameters to display additional weather data .
  • Integrate real-time notifications and warning signals.

As you can see, the basic app building process is pretty simple and straightforward. By following the above process to leverage the power of a weather API, even beginner-level developers can get their weather application up and running in a matter of minutes.