forked from pavanbelagatti/weather-app-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
37 lines (31 loc) · 906 Bytes
/
server.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
import express from "express";
import cors from "cors";
import dotenv from "dotenv";
import fetch from "node-fetch";
const app = express();
const port = process.env.PORT || 5004;
//registering middlewares
dotenv.config();
app.use(express.json());
app.use(cors());
//registering routes
app.get("/", (req, res) => {
res.status(200).send("Weather API is Running!");
});
//fetching weather forecast for a particular city
app.get("/weather", async (req, res) => {
if (!req.query.city) {
res.status(404).json("City is missing");
} else {
let city = req.query.city;
const response = await fetch(
`http://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${process.env.API_KEY}`
);
const data = await response.json();
res.status(200).json(data);
}
});
//creating server
app.listen(port, () => {
console.log(`server is up on ${port}`);
});