Introduction
In this blog, we see how to build a weather app using open weather APIs.
Prerequisite
Setup Folder
- Open console and create a new folder using the below command
# mkdir weather
- Change to the current directory
# cd weather
Setup Node In Folder
For building projects in Node, we have to setup up a Node environment in our folder.
- Type the following command to set up Node
# npm init -y
This will generate a package.json file, which means that Node is set up in the folder .
Install Package
We add a package that will be needed to build the application.
- For adding package type following command
# npm install body-parser ejs express openweather-apis
A little description of the packages we installed:
- body-parser: extracts the entire body portion of an incoming request stream and exposes it on req.body.
- ejs: ejs or embedded Javascript templating is a templating engine used by node.js.ejs. It is a simple template language used to generate HTML markup with plain Javascript.
- express: it is a web framework for node.js.The complete application is built on it
- openweather-apis: with this package, we can consume the weather APIs.
Add Folder
- Add a new folder named views
- Add new file in it and name it home.ejs.
- <html>
- <head>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <title>Weather App</title>
- <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/css/bulma.min.css">
- <script defer src="https://use.fontawesome.com/releases/v5.3.1/js/all.js"></script>
- </head>
- <body>
- <div class="container" style="margin-top: 20px;width: fit-content;">
- <div class="field has-addons">
- <div class="control">
- <form action="/" method="POST">
- <input class="input" type="text" placeholder="Enter City" name="city">
- <input class="button is-info" value="Submit" type="submit">
- </form>
- </div>
- </div>
- </div><br><br>
- <% if(temp) {%>
- <div class="table">
- <center>
- <h3><b>City Weather/Temperature Details</b></h3><br>
- <table class= 'table is-bordered is-striped is-narrow is-hoverable table is-bordered' style="font-size: large;font-weight: bolder;">
- <tbody>
- <tr><td>City</td><td><%=temp.name%></td></tr>
- <tr><td>Weather</td><td><%=temp.weather[0].main%></td></tr>
- <tr><td>Temperature</td><td><%=temp.main.temp%></td></tr>
- </tbody>
- </table>
- </center>
- </div>
- <%}%>
- </body>
- </html>
Add Starting Point
We have to provide the entry/starting point to our aplication from where the application will start.
- Now add a new file and name it app.js.
- Now open the package.json file and in script add "start":"app.js".This will tell our application about the entry/start point.
- Now get register https://openweathermap.org/api and get your secret key from your dashboard.
app.js
- var express = require('express');
- var bodyParser = require('body-parser');
- var weather = require('openweather-apis');
-
-
-
- var app = express();
-
-
- app.set('view engine','ejs');
-
-
- app.use(bodyParser.urlencoded({extended:false}));
-
-
- app.get('/',function(req,res){
- res.render('home',{temp:null});
- });
-
- app.post('/',function(req,res){
- //set the city whose temperature we want to see
- weather.setCity(req.body.city);
- // you have to provide the secretkey you get on your dashboard of your openweathermap
- weather.setAPPID();
-
- //it will provide us all the data of that particular city in json format.
- weather.getAllWeather(function(err,temp){
- console.log(temp);
- res.render('home',{temp:temp});
- });
- });
-
- var port = process.env.PORT || 3000;
- app.listen(port,function(){
- console.log('server running at '+port);
- });
Download code from here.