-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
39 lines (29 loc) · 987 Bytes
/
server.js
File metadata and controls
39 lines (29 loc) · 987 Bytes
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
import express from 'express';
import axios from 'axios';
const app = express();
app.set("view engine", "ejs");
app.use(express.static("public"));
app.get("/", (req,res) => {
res.render("index", {weather: null, error: null});
});
app.get("/weather", async (req,res) => {
const city = req.query.city;
const apiKey = "bb7f28c1af7023a005d604318ccdf476";
const url = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${apiKey}`;
let weather;
let error = null;
try{
const response = await axios.get(url);
weather = response.data;
}catch (error) {
weather = null;
error = "Error, Please try again";
}
res.render("index", {weather, error});
})
// This below checks the enviromental Variable before assigning the port
// const port = process.env.PORT || 5000;
const port = 5000;
app.listen(port, ()=> {
console.log(`Server is running on port ${port}`);
});