-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
44 lines (35 loc) · 1.6 KB
/
app.js
File metadata and controls
44 lines (35 loc) · 1.6 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
const express = require("express");
const https = require("https");
const bodyParser = require("body-parser");
const app = express();
app.use(bodyParser.urlencoded({extended: true}));
app.listen(3000, function() {
console.log("Server is running on port 3000.");
});
app.get("/", function(req, res) {
res.sendFile(__dirname + "/index.html");
});
app.post("/", function(req, res) {
const query = req.body.zipCode;
const apiKey = "";
const unit = "imperial";
const url = `https://api.openweathermap.org/data/2.5/weather?zip=${query}&appid=${apiKey}&units=${unit}`;
https.get(url, function(response) {
console.log(response.statusCode);
response.on("data", function(data) {
// Turns the data that's currently a string into a JSON object.
const weatherData = JSON.parse(data);
// console.log(weatherData);
const temp = weatherData.main.temp;
// The weather object is an array that contains 1 item which is why we access it using [0].
const weatherDescription = weatherData.weather[0].description;
const city = weatherData.name;
const icon = weatherData.weather[0].icon;
const imageURL = `http://openweathermap.org/img/wn/${icon}@2x.png`;
res.write(`<h1 style="font-family: monospace;">The temperature in ${city} is ${temp} degrees fahrenheit.</h1>`);
res.write(`<h2 style="font-family: monospace;">The weather currently is ${weatherDescription}.</h2>`);
res.write(`<img src="${imageURL}">`);
res.send();
})
});
});