-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
49 lines (43 loc) · 1.51 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
var express = require("express");
var path = require("path");
var cookieParser = require("cookie-parser");
var logger = require("morgan");
var axios = require("axios");
var app = express();
// view engine setup
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "ejs");
app.use(logger("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
// Serve the public folder as static files
// app.use(express.static("public"));
app.use(express.static(path.join(__dirname, "public")));
// Render the index template with default values for weather and error:
app.get("/", (req, res) => {
res.render("index", { weather: null, error: null });
});
// Handle the /weather route
app.get("/weather", async (req, res) => {
// Get the city from the query parameters
const city = req.query.city;
const apiKey = "9496b1253db5f8718358a50073437bd6";
const APIUrl = `https://api.openweathermap.org/data/2.5/weather?q=${city}&units=imperial&appid=${apiKey}`;
let weather;
let error = null;
try {
const response = await axios.get(APIUrl);
weather = response.data;
} catch (error) {
weather = null;
error = "Error! Please try again.";
}
// Render the index template with the weather data and error message:
res.render("index", { weather, error });
});
// Start the server and listen on port 3000 or value of PORT environment variable
const port = process.env.port || 3000;
app.listen(port, () => {
console.log(`App running on port ${port}`);
});