Weather App with Node.js

Swati Wuppuluri
3 min readSep 2, 2020

Github: https://github.com/swati1707/node-weatherapp

Introduction: In this tutorial we will learn how to build a simple weather application using Node.js.

Node is an asynchronous event driven JavaScript runtime built upon Chrome’s V8 JavaScript engine. It’s designed to build scalable network applications.

Prerequisites:

  • Node.js installed on your machine
  • Basics of node
  • An openweathermap API key

Now let’s get started…

Step 1:- Create a new directory

Open command prompt and run the commands:

mkdir node-weather
cd node-weather

This creates a directory for your project.

Step 2:- Initialize your project.

Now it’s time to initialize your project and link it to npm. To initialize your project, run the below command:

npm init

A package.json file will be created in your folder which references all the npm packages you’ll download for your project.

Step 3:- Install the request module

To make a request to the weather API you need to install the request module. Run the below command to install request module.

npm install request

Step 4:- Create a file named app.js

Write the following code in this file:

var http = require("http")var address = process.argv[2]var url = `http://api.openweathermap.org/data/2.5/weather?q=${address} &units=metric&appid=83985bda175bf0a119cefd45aa1ed4a3`var server = http.createServer(function(request,response){var request = require("request");request(url, function(error, res, body) {var data = JSON.parse(body);if(!address){response.write("<html><body><div id='container'>");response.write("<h1>" + 'Please enter city name'+ "</h1>");response.write("</div></body></html>");response.end();}response.write("<html><body><div id='container'>");response.write("<h1>"+ 'City Name : ' + data['name'] + '<br>' + "</h1>");response.write("<h2>"+ 'Temperature : ' + data.main['temp'] + '<br>' + "</h2>");response.write("<h2>"+ 'Sunset Time : ' + new Date(data.sys['sunset']*1000) + "</h2>");response.write("</div></body></html>");response.end();});}).listen(8081);

In the first line, an http variable is declared which contains the http module.

Then in the third line, the url variable contains the url of the openweathermap API.

var url = `http://api.openweathermap.org/data/2.5/weather?q=london,uk&units=metric&appid=83985bda175bf0a119cefd45aa1ed4a3`

This url gives us the weather data of London.

Step 5:- Create a server

Next step is to create a server. The createServer method of http module is used to create a server. Inside the createServer method, a request variable is declared which contains the request package. Then comes the request method that is built-in to the request package. It contains two arguments. First is the url to which the request is made and second is a function which is responsible to display the data or errors if any. This function again has few parameters namely error, response and body. The JSON data received from the API is a bit messed up. So the next step is to parse the JSON data into a javascript object.

Step 6:- Display the output.

Now that you’ve the data from the API, you can display it in html format.

Below is the code to display your weather data.

response.write("<html><body><div id='container'>");response.write("<h1>"+ 'City Name : ' + data['name'] + '<br>' + "</h1>");response.write("<h2>"+ 'Temperature : ' + data.main['temp'] + '<br>' + "</h2>");response.write("<h2>"+ 'Sunset Time : ' + new Date(data.sys['sunset']*1000) + "</h2>");response.write("</div></body></html>");
response.end();

The listen method starts a server and listens on port 8081 and responds with the weather data.

Step 7:- Run the app

Type the command in your terminal:

node app.js

After running the command, load the url https://localhost:8081 in your browser.

And here you go…

You’ve successfully fetched data from the API and displayed it.

But the data you’ll get from the url mentioned in Step-4 is only of one city.

What if you want the weather data of any other city ?

Now that’s where address variable (declared on line 2) comes into the picture.

var address = process.argv[2]

The process.argv returns the arguments you pass while running the node app.

For eg:-

node app.js title

Here title becomes one argument. And to access this argument you’ll use process.argv[2].

So the same way you can mention the city and country name while running the app which will be assigned to the address variable and you can pass that address variable in the url (as done in the code above in app.js file). That way you can get the weather data of the city name you enter while running the app.

Now what if user doesn’t enter any city name while running the app.

For that purpose, an if condition is added to check whether the address variable contains any city name or not.

if(!address){response.write("<html><body><div id='container'>");response.write("<h1>" + 'Please enter city name'+ "</h1>");response.write("</div></body></html>");response.end();}

And accordingly a message will be displayed to the user to enter the city name.

That’s it, you’re done. You’ve successfully created a weather app using Node.js .

--

--