-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
56 lines (46 loc) · 1.42 KB
/
server.js
File metadata and controls
56 lines (46 loc) · 1.42 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
45
46
47
48
49
50
51
52
53
54
55
require('dotenv').config();
const port = 3000;
const express = require('express');
const server = express();
const maxFreeDays = 3; //free api subscription only allows for 3 days of forecast :(
server.use(express.static('public'));
server.use(express.json());
server.get('/info/:dynamic', (req, res) => {
const {lat, long} = req.query;
console.log(lat);
console.log(long);
apiCallForecast(lat, long)
.then(result => {
console.log("Result:", result);
res.status(200).json
({ result});
})
.catch(error => {
console.error("Error:", error);
});
});
server.listen(port, function(error) {
if(error){
console.log("something went wrong dumass", error);
} else{
console.log("all good " + port);
}
})
async function apiCallCurrent(lat, long){
const data = await fetch(`https://api.weatherapi.com/v1/current.json?key=${process.env.API_KEY}&q=${lat},${long}`, {
method: `GET`
});
console.log(data);
const info = await data.json();
console.log(info);
return(info);
}
async function apiCallForecast(lat, long){
const data = await fetch(`https://api.weatherapi.com/v1/forecast.json?key=${process.env.API_KEY}&q=${lat},${long}&days=${maxFreeDays}`, {
method: `GET`
});
console.log(data);
const info = await data.json();
console.log(info);
return(info);
}