-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
41 lines (30 loc) · 1.53 KB
/
Copy pathindex.js
File metadata and controls
41 lines (30 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const request = require('request'); // Request module for API Call
var reader = require('readline'); // Readline module for the user input
var req = reader.createInterface({
input: process.stdin,
output: process.stdout
});
req.question("Enter your City Name: ", function(answer) {
//Taking input from user
let apiKey = '******************************';
//Enter your API Key here (Replace '***'s with your API Key)
let url = `http://api.openweathermap.org/data/2.5/weather?q=${answer}&units=imperial&appid=${apiKey}`
//The URL used to make the HTTP request, '?' indicates the start of the Query. These are set of Key & Values seperated by a '='. Different Keys & Values pair are seperated by a '&'
req.close();
request(url, function (err, response, body) {
// In the request package we pass in the URL and a function with three arguments
if(err){
// Checks for an error in our request
console.log('error:', error);
// If there is an error we log the error
} else {
let weather = JSON.parse(body)
// There will be unwanted quotation marks in the output, so we need to parse it to a JSON(JavaScript Object Notation) so it's easy to accces.
// Now we can access the JavaScript 'weather' object, to access the data within the object and display it.
let message1 = `\nCountry: ${weather.sys.country} \nCity: ${weather.name} \nTemperature: ${weather.main.temp}`;
let message2 = `\nLocation is... \nLongitutde: ${weather.coord.lon} \nLattitude: ${weather.coord.lat}`;
console.log(message1);
console.log(message2);
}
});
});