-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
196 lines (147 loc) · 5.33 KB
/
index.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
const express = require('express');
const app = express();
const path = require('path');
const ejsMate = require('ejs-mate');
const axios = require('axios');
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'))
const PORT = process.env.PORT || 1990;
app.engine('ejs', ejsMate);
app.use(express.static("public")); // to locate css and other files
// home page route
app.get('/', (req, res) => {
res.render('home')
})
// films route
app.get('/films', async (req, res) => {
const starWarsFilms = await axios.get(`https://swapi.dev/api/films/`)
// console.log(tvDat.data);
const filmDat = starWarsFilms.data.results;
res.render('films', { filmDat });
})
// people route
app.get('/people', async (req, res) => {
// use a base url to request people
baseURL = "https://swapi.dev/api/people/"
let starWarsPeople = await axios.get(baseURL);
let peopleDat = [];
peopleDat.push(starWarsPeople.data.results);
let nextPageURL = starWarsPeople.data.next;
// console.log(nextPageURL);
while(nextPageURL){
starWarsPeople = await axios.get(nextPageURL);
const data = starWarsPeople.data.results;
// console.log(data);
// console.log(data.length);
peopleDat.push(data)
nextPageURL = starWarsPeople.data.next;
console.log(nextPageURL);
}
console.log(peopleDat.length);
res.render('people', { peopleDat });
})
// vehicles route
app.get('/vehicles', async (req, res) => {
const baseURL = "https://swapi.dev/api/vehicles/";
let starWarsVehicle = await axios.get(baseURL);
let vehicleDat = [];
vehicleDat.push(starWarsVehicle.data.results);
let nextPageURL = starWarsVehicle.data.next;
// console.log(nextPageURL);
while(nextPageURL){
starWarsVehicle = await axios.get(nextPageURL);
const data = starWarsVehicle.data.results;
// console.log(data);
// console.log(data.length);
vehicleDat.push(data)
nextPageURL = starWarsVehicle.data.next;
console.log(nextPageURL);
}
console.log(vehicleDat.length);
res.render('vehicles', { vehicleDat });
})
// people detail route
app.get('/people/:name', async (req, res) => {
const name = req.params.name;
console.log(name);
const starWarPeople = await axios.get(`https://swapi.dev/api/people/?search=${name}`);
const peopleDetail = starWarPeople.data.results;
console.log(peopleDetail);
// get film details this character was in
const filmUrl = peopleDetail[0].films;
console.log(filmUrl);
let filmSilo = [];
for (let url of filmUrl) {
const filmDat = await axios.get(url);
const filmTitle = filmDat.data.title;
console.log(filmTitle);
filmSilo.push(filmTitle);
}
// get vehicles this character piloted
const vehicleUrl = peopleDetail[0].vehicles;
console.log(vehicleUrl);
let vehicleSilo = [];
for (let url of vehicleUrl) {
const vehicleDat = await axios.get(url);
const vehicleTitle = vehicleDat.data.name;
console.log(vehicleTitle);
vehicleSilo.push(vehicleTitle);
}
console.log(vehicleSilo);
res.render('peopleDetail', { peopleDetail, filmSilo, vehicleSilo })
})
// films detail route - use search based on the name of the film
app.get('/films/:filmName', async (req, res) => {
const filmTitle = req.params.filmName;
console.log(filmTitle);
const filmDat = await axios.get(`https://swapi.dev/api/films/?search=${filmTitle}`);
const filmDatClean = filmDat.data.results[0];
// get character/people data
const people = filmDat.data.results[0].characters;
const peopleList = [];
for (let peopleUrl of people) {
const peopleDat = await axios.get(peopleUrl);
const peopleName = peopleDat.data.name;
peopleList.push(peopleName);
}
// get vehicles data
const vehicles = filmDat.data.results[0].vehicles;
const vehicleList = [];
for (let vehicleUrl of vehicles) {
const vehicleDat = await axios.get(vehicleUrl);
const vehicleName = vehicleDat.data.name;
vehicleList.push(vehicleName);
}
console.log(vehicleList);
res.render('filmDetail', { filmDatClean, peopleList, vehicleList });
})
// vehicles detail router
app.get('/vehicles/:vehicleName', async (req, res) => {
const vehicleName = req.params.vehicleName;
console.log(vehicleName);
const vehicleDat = await axios.get(`https://swapi.dev/api/vehicles/?search=${vehicleName}`);
const vehicleDatClean = vehicleDat.data.results[0];
console.log(vehicleDatClean);
// get pilot/people data
const pilots = vehicleDat.data.results[0].pilots;
const pilotList = [];
for (let pilotUrl of pilots) {
const pilotDat = await axios.get(pilotUrl);
const pilotName = pilotDat.data.name;
pilotList.push(pilotName);
}
// get film data
const films = vehicleDat.data.results[0].films;
const filmList = [];
for (let filmUrl of films) {
const filmDat = await axios.get(filmUrl);
console.log(filmDat);
const filmName = filmDat.data.title;
filmList.push(filmName);
}
console.log(filmList);
res.render('vehicleDetail', { vehicleDatClean, pilotList, filmList });
})
app.listen(PORT, () => {
console.log(`app running on port ${PORT}`);
})