DEV Community ๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ‘จโ€๐Ÿ’ป

Cover image for Weather Application using Node.js โ„๏ธ
Sanjula De Alwis
Sanjula De Alwis

Posted on

Weather Application using Node.js โ„๏ธ

Before the application build lets talk about what is Node.js?

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.

Why Node.js?

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.

Table of Contents :

  • Install NodeJS
  • Getting API
  • Consuming API
  • Show API

nodejs

Cool! Lets Start the project ๐Ÿ™Œ

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 npm .

3.Get the Weather API from here.

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

api

Now, What you need is

  • Country Name (you can use any country, its up to you!)
  • Country Code (Go to this link and get the Code of your country)
  • API Key (Sign Up this link and they will give you the API key)

Cool! Itโ€™s time to develop this application ๐Ÿ’ป

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

  • Enter all the things. Now your project has package.json file.
  • Now create a new file name it app.js.

Go back to the terminal and install certain packages :

npm install request -S

Go to app.js and copy below code :

apicode

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!

Top comments (0)

Layoffs: Itโ€™s Okay to Not Be Okay