-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
69 lines (43 loc) · 1.39 KB
/
server.js
File metadata and controls
69 lines (43 loc) · 1.39 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
56
57
58
59
60
61
62
63
64
65
66
'use strict';
// Requires
const express = require('express');
require('dotenv').config();
const weatherData = require(`./data/weather.json`);
const axios = require('axios');
// Use
const app = express();
const PORT = process.env.PORT || 3002;
// if something is wrong 3002 with run with the server.
// Routes
// app.get('/', (request, response) => {
// response.send('HI');
// });
app.get('/weather', async (req, res) => {
console.log(req);
const { lon, lat } = req.query;
let search = req.query.searchQuery;
// res.send(`${lon}`, `${lat}`, `${search}`);
try {
let weatherUrl = `https://api.weatherbit.io/v2.0/forecast/daily/?${process.env.WEATHER_API_KEY}&1and=en&lat=${lat}&lon=${lon}&days=3`;
let weather = await axios.get(weatherUrl);
let weather2 = weather.data.data.map(f => new Forecast(f));
res.status(200).send(weather2);
} catch (error) {
console.log(error);
res.status(500).send(error.message);
}
// Got help to reformat from david and michael. it made more sense to me than the way i had it so i changed iy :)
});
// app.get('*', (resquest, response) => {
// response.send('Its not here!!');
// });
class Forecast {
constructor(days) {
this.date = days.valid_date;
this.description = days.weather.description;
this.city = days.data.city_name;
}
}
// Errors
// Listen
app.listen(PORT, () => console.log(`listening on ${PORT}`));