-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
103 lines (82 loc) · 2.8 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
'use strict'
//Load environdment Vars from .env
require('dotenv').config();
// App Dependancies
const express = require ('express');
const cors = require('cors');
const superagent = require('superagent');
//App Setup
const PORT = process.env.PORT || 3000;
const app = express();
app.use(cors());
//API routes
//locations
app.get('/location', (request, response) => {
searchToLatLong(request.query.data)
.then(location => response.send(location))
.catch(error => handleError(error, response));
});
//weather
app.get('/weather', (request, response) => {
getWeather(request.query.data)
.then(data => response.send(data))
.catch(error => handleError(error, response))
})
//meetups
app.get('/meetups', (request, response) => {
getMeetups(request.query.data)
.then(data => response.send(data))
.catch(error => handleError(error, response))
})
//catch-all error handler
app.use('*', handleError);
// Make sure server is listening for requests
app.listen(PORT, () => console.log(`App is up on ${PORT}`))
// Helper Functions
// Err Handler
function handleError(err, res) {
//console.error(err);
if (res) res.status(500).send('Sorry, something went terribly wrong. Toodles!');
}
//creates a new object with our geocode data
function searchToLatLong(query) {
const geoUrl = `https://maps.googleapis.com/maps/api/geocode/json?address=${query}&key=${process.env.GEOCODE_API_KEY}`;
return superagent.get(geoUrl)
.then(res => new Location(query, res))
.catch(error => handleError);
}
function Location(query, res) {
this.search_query = query;
this.formatted_query = res.body.results[0].formatted_address;
this.latitude = res.body.results[0].geometry.location.lat;
this.longitude = res.body.results[0].geometry.location.lng;
}
// creates array of objects with our weather data
function getWeather(location) {
const weatherUrl = `https://api.darksky.net/forecast/${process.env.DARKSKY_API_KEY}/${location.latitude},${location.longitude}`;
return superagent.get(weatherUrl)
.then(res => {
return res.body.daily.data.map(day => new Weather(day));
})
.catch(error => handleError);
}
function Weather(day) {
this.forecast = day.summary;
this.time = new Date(day.time *1000).toString().slice(0,15);
}
//creates array of objects with our meetup data
function getMeetups(location) {
let meetupsURL = `https://api.meetup.com/find/groups?&query=${location.search_query}&page=20&key=${process.env.MEETUPS_API_KEY}`;
return superagent.get(meetupsURL)
.then(res => {
console.log(res.body[0]);
return res.body.map(event => new Meetups(event))
})
.catch(error => handleError)
}
function Meetups(event){
this.name = event.name;
this.link = event.link;
this.creation_date = new Date(event.created *1000).toString().slice(0,15);
this.host = event.organizer.name;
}