-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
77 lines (57 loc) · 2.85 KB
/
script.js
File metadata and controls
77 lines (57 loc) · 2.85 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
const express = require("express");
const https = require("https");
//body-parser module allows us to look through and grab specific data inside a form post request
const bodyParser = require("body-parser");
const app = express();
app.use(bodyParser.urlencoded({extended: true}))
//Gets the request made to localhost URL and is used to respond
app.get("/", function(req,res){
//Remember the __dirname is used to reach the main folder no matter its location
//Then we add the specific location we want to reach
res.sendFile(__dirname + "/index.html");
});
//This is the method used to get the post request made by the form
app.post("/", function(req,res){
// console.log("Post request received");
// console.log(req.body.cityName);
//res.send("Server is up and running");
//The req is retrieved from the form
//then we access its body
//and target the cityname
const location = req.body.cityName;
const apiKey = "acece669eddaa0b15d76de2de1f2cee6";
//URL creasted with postman, the https:// must be written manually.
const url = "https://api.openweathermap.org/data/2.5/weather?q="+location+"&appid="+apiKey+"&unit=metric";
//This is a method that makes an https request and retrieves the data from that URL
https.get(url, function(response){
//console.log(response);
console.log(response.statusCode);
//The response.on() fetches the data retrieved from an https request.
response.on("data", function(data){
//The JSON.parse() converts hexa decimal data to a Javascript object.
//The reverse of this method is JSON.stringify() that will convert it to hexa.
const weatherData = JSON.parse(data);
//Here we tap into specific data in the Javascript pbject.
const temp = weatherData.main.temp;
const humidity = weatherData.main.humidity;
const weatherDes = weatherData.weather[0].description;
//Here we get the weather icon code
const weatherIcon = weatherData.weather[0].icon;
//Then we fetch it through the weather API
const imageURL = "http://openweathermap.org/img/wn/"+weatherIcon+"@2x.png";
console.log("The temperature is : " + temp);
console.log("The humidity is : " + humidity);
console.log("The weather is "+weatherDes);
res.write("<h1>The temperature in "+location+" is : "+temp+"</h1>");
res.write("\n<p>The humidity is : "+humidity+"</p>");
res.write("\n<p>The weather description is : "+weatherDes+"</p>");
//Here we display the image we fetched
res.write("<img src = "+imageURL+">");
res.send();
//console.log(weatherData);
})
});
});
app.listen(3000 , function() {
console.log("Server is running on port 3000.");
});