-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
70 lines (53 loc) · 2.14 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const express = require("express");
const https = require("https");
const bodyParser = require("body-parser");
const app = express();
app.set("view engine", "ejs");
const port = 3000;
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static("public"));
app.get("/", function(req, res){
res.sendFile(__dirname + "/index.html");
})
app.post("/", function(req, res){
const apiKey = "62d4690f0406a37b787fd8be4934041cfa";
const query = req.body.cityName;
const unit = "metric";
const url = "https://api.openweathermap.org/data/2.5/weather?q=" + query + "&appid=" + apiKey + "&units=" + unit;
https.get(url, function(response){
console.log("Status Code:", response.statusCode);
response.on("data", function(data){
const weatherData = JSON.parse(data);
const temp = weatherData.main.temp;
const weatherDescription = weatherData.weather[0].description;
const icon = weatherData.weather[0].icon
const imageURL = "http://openweathermap.org/img/wn/" + icon + "@2x.png";
const event = new Date();
const options = {
weekday: "long",
hour: "2-digit",
minute: "2-digit",
timeZone: "Africa/Lagos"
}
const moment = event.toLocaleDateString("en-us", options);
// res.writeHead(200, { 'Content-Type': 'text/html' });
// res.write("<h1>The weather in " + query + " today is " + temp + " degree celsius</h1>");
// res.write("<h2>It is " + weatherDescription + "</h2>");
// res.write("<img src=" + imageURL + ">");
// res.write("<br>");
// res.write("<br>");
// res.write("<a href='/'>Back to home</a>");
// res.send();
res.render("result", {
degree: temp,
description: weatherDescription,
city: query,
image: imageURL,
time: moment
});
});
})
});
app.listen(process.env.PORT || port, function(){
console.log("App is running on port " + port);
});