-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
57 lines (47 loc) · 1.16 KB
/
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
const PORT = 8001;
const express = require("express");
const cors = require("cors");
const axios = require("axios");
require("dotenv").config();
const API_KEY = process.env.OPEN_WEATHER_API_KEY;
const server = express();
server.use(cors());
server.get("/coordinates", (req, res) => {
const params = new URLSearchParams({
q: req.query.location,
limit: 5,
appid: API_KEY,
}).toString();
const options = {
method: "GET",
url: `http://api.openweathermap.org/geo/1.0/direct?${params}`,
};
axios
.request(options)
.then((response) => {
res.json(response.data);
})
.catch((error) => {
console.error(error);
});
});
server.get("/weather", (req, res) => {
const params = new URLSearchParams({
lat: req.query.lat,
lon: req.query.lon,
appid: API_KEY,
}).toString();
const options = {
method: "GET",
url: `https://api.openweathermap.org/data/2.5/weather?${params}`,
};
axios
.request(options)
.then((response) => {
res.json(response.data);
})
.catch((error) => {
console.error(error);
});
});
server.listen(PORT, () => console.log(`Server is running on port ${PORT}`));