-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.js
139 lines (109 loc) · 3.45 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//////////// Modules /////////////
const dotenv = require('dotenv');
dotenv.config();
const express = require('express');
const app = express();
const port = process.env.PORT
// if (process.env.DEVELOPMENT) {
// const morgan = require('morgan');
// }
const bodyParser = require('body-parser');
const axios = require('axios');
const geoJSON = require('./library/geoJSON_module');
const session = require('express-session');
const { v4: genuuid } = require('uuid');
const indexRouter = require('./routes/index')
const attractionsRouter = require('./routes/attractions')
let inputForFinalPage = []
//////////// Middleware /////////////
app.set('view engine', 'ejs');
// if (process.env.DEVELOPMENT) {
// app.use(morgan('combined'));
// }
app.use(bodyParser.json());
app.use(express.static('public'));
//////////// routing /////////////
// app.use('/', indexRouter)
// app.use('/attractions', attractionsRouter)
//////////// Session setup /////////////
app.use(session({
name: 'SessionCookie',
genid: function (req) {
console.log('session id created')
return genuuid() // use UUIDs for session IDs
},
secret: 'tom cat',
resave: false,
saveUninitialized: true,
cookie: { maxAge: 3600000 }
}))
var sess;
//////////// Routes /////////////
app.get('/', (req, res) => {
sess = req.session;
sess.starredItems = [];
inputForFinalPage = [];
sess.apiResults = "";
res.render('welcome_page')
})
app.get('/index', (req, res) => {
sess = req.session;
if (sess.apiResults) {
let arrOfPlaces = sess.apiResults.data.places
let arrOfInstances = []
arrOfPlaces.forEach(place => {
if (place.perex !== null){
let instancePlace = { id: place.id, name: place.name, location: place.location, description: place.perex, image: place.thumbnail_url, timeToSpend: place.duration_estimate}
arrOfInstances.push(instancePlace)
}
})
res.render('index', {
attractions: arrOfInstances
})
} else {
let url = `https://api.sygictravelapi.com/1.2/en/places/list?parents=city:381&categories=${req.query.category}&limit=20`
axios.get(url, {
headers: {
'x-api-key': process.env.API_KEY_SYGIC
}
}).then(response => {
// res.json(response.data)
// clean up the date to provide us the relevant information
let arrOfPlaces = response.data.data.places
let arrOfInstances = []
arrOfPlaces.forEach(place => {
if (place.perex !== null){
let instancePlace = { id: place.id, name: place.name, location: place.location, description: place.perex, image: place.thumbnail_url, timeToSpend: place.duration_estimate}
arrOfInstances.push(instancePlace)
}
})
sess.apiResults = response.data;
// res.json(arrOfInstances)
res.render('index', {
attractions: arrOfInstances
})
})
}
})
app.get('/itinerary', (req, res) => {
res.render('itinerary',{selectedLocs: inputForFinalPage})
})
app.post('/attractions', (req, res) => {
sess = req.session
inputForFinalPage.push(req.body.attraction)
// console.log(inputForFinalPage)
res.send(inputForFinalPage)
})
app.get('/attractions', (req, res) => {
sess = req.session
res.send(inputForFinalPage)
})
// geoJSON output
app.get('/api/geojson', (req, res) => {
sess = req.session;
res.json(geoJSON.convertToGeoJSON(sess.apiResults));
})
//////////// SERVER LISTENNING /////////////
app.listen(port, () => {
console.log(`listening to port ${port}`);
})