Weather Application using Node.js ❄️
Tags: node, javascript
Published: 2020-05-11 05:39:44

<h1>Before the application build lets talk about what is Node.js?</h1>

Node.js is a JavaScript run time environment. Sounds great, but what does that mean? How does that work?
The Node run-time environment includes everything you need to execute a program written in JavaScript.

<b>Why Node.js?<b>

<p> Here’s a formal definition as given on the official Node.js website:
Node.js® is a JavaScript runtime built on Chrome’s V8 JavaScript engine.
Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient.
Node.js’ package ecosystem, npm, is the largest ecosystem of open source libraries in the world. </p>

<b>Table of Contents :</b>
<ul>
<li>Install NodeJS</li>
<li>Getting API</li>
<li>Consuming API</li>
<li>Show API</li>
</ul>

![nodejs](https://miro.medium.com/max/875/0*6-chlaP2EwqR0_sF.png)

<b>Cool! Lets Start the project 🙌</b>


1.Open a command prompt and type:

```mkdir weatherApp
cd weatherApp```
These commands are universal for whatever OS you’ll be running. The former will create a new directory inside the directory you are currently in, mkdir = “make directory”. The latter will change into this newly created directory, cd = “change directory”. Hard-core windows users can calm down, this will work for you guys too, as it is equivalent to creating a new folder within your file system… only more fancy.

2.Initialize your project and link it to <a href ="https://www.npmjs.com/"> npm </a> .

3.Get the Weather API from here.

This website basically give you the weather based on your city.

![api](https://miro.medium.com/max/794/1*d4TFL5evELc0yJHJQzFL4A.png)

Now, What you need is
<ul>
<li>Country Name (you can use any country, its up to you!)</li>
<li>Country Code (Go to this link and get the Code of your country)</li>
<li>API Key (Sign Up this link and they will give you the API key)</li>
</ul>

<b>Cool! It’s time to develop this application 💻</b>

Now, go back to you project folder(weatherApp) open this project using VS Code or any other IDE you like. Go to the terminal make shuer you are in the right directory eg: …/weatherApp/ .

Open the terminal and type this below code :

```npm init```

<ul>
<li>Enter all the things. Now your project has package.json file.</li>
<li>Now create a new file name it app.js.</li>
</ul>

Go back to the terminal and install certain packages :

```npm install request -S```

Go to app.js and copy below code :

![apicode](https://miro.medium.com/max/875/1*iB4hOUKrprpwJv_RDOBZhA.jpeg)

```
var http = require('http');
var url = 'put here your API Key URL';
var server = http.createServer(function ( request, response ) {
// All logic will go here
var request = require('request');
request( url , function(err, res, body) {
var data = JSON.parse(body);
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) + '<br>'+ "</h2>");
response.write("</div></body></html>");
response.end();
});
}).listen(8081);
```

Go back to you terminal and run this application :

```node app.js```

You can see the output with your :

City Name :
Temperature :
Sunset Time :

Thank you!