-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
46 lines (35 loc) · 1.38 KB
/
app.js
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
const express = require("express");
const https = require("https");
const bodyParser = require("body-parser");
const token = require("./weatherToken.json");
const app = express();
app.use(bodyParser.urlencoded({extended: true}));
app.get('/', (req, res)=>{
res.sendFile(__dirname + "/index.html");
});
app.post("/", function(req, res){
console.log("Post requested received.");
const query = req.body.cityName;
const apiKey = token.token;
const unit = "metric";
const url = "https://api.openweathermap.org/data/2.5/weather?q="+query+"&units="+unit+"&appid=" + apiKey;
https.get(url, function(response){
console.log(response.statusCode);
response.on("data", function(data){
// console.log(data); //returns hexadecimal data
const weatherData = JSON.parse(data);
// console.log(weatherData);
const temp = weatherData.main.temp;
const weatherDescription = weatherData.weather[0].description;
const iconURL = "http://openweathermap.org/img/wn/" +weatherData.weather[0].icon+"@2x.png";
res.write("<h1>The temperature in "+query+" is "+temp+" degrees is Celcius.</h1>");
res.write("<p>The weather is currenctly "+weatherDescription+"</p>");
res.write("<img src='"+iconURL+"'/>");
res.send();
});
});
// res.send("Server up and running.");
});
app.listen(3000, ()=>{
console.log("Server is running on port 3000.");
});