-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
59 lines (53 loc) · 1.28 KB
/
index.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
const express = require("express");
const app = express();
const hb = require("express-handlebars");
const https = require("https");
app.engine("handlebars", hb());
app.set("view engine", "handlebars");
app.use(express.static("./public"));
app.use(function(req, res, next) {
if (req.headers["x-forwarded-proto"] === "https") {
res.redirect("http://" + req.hostname + req.url);
} else {
next();
}
});
let body = "";
const req = https.request(
{
method: "GET",
host: "api.openweathermap.org",
path:
"/data/2.5/weather?q=berlin,de&appid=" + process.env.key + "&units=metric"
},
resp => {
if (resp.statusCode != 200) {
console.log(resp.StatusCode);
} else {
resp
.on("data", chunk => {
body += chunk;
})
.on("end", () => {
try {
body = JSON.parse(body);
console.log(body);
} catch (err) {
console.log(err);
}
})
.on("error", err => console.log(err));
}
}
);
req.on("error", err => console.log(err));
req.end();
app.get("/", (req, res) => {
res.render("welcome", {
name: body.name,
temp: body.main.temp,
description: body.weather[0].description,
layout: "main"
});
});
app.listen(process.env.PORT || 8080);