-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
396 lines (368 loc) · 10.1 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
const mongoose = require('mongoose');
const Models = require('./models.js');
const Movies = Models.Movie;
const Users = Models.User;
mongoose.connect(process.env.CONNECTION_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
// mongoose.connect locally
// mongoose.connect('mongodb://localhost:27017/myFlixDB', {
// useNewUrlParser: true,
// useUnifiedTopology: true,
// });
const express = require('express'),
morgan = require('morgan'),
fs = require('fs'),
path = require('path'),
bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json());
const cors = require('cors');
let allowedOrigins = [
'https://myflix-501fc4.netlify.app',
'https://myflix-501fc4.netlify.app/',
'https://lkarow.github.io',
'https://lkarow.github.io/',
'https://lkarow.github.io/myFlix-Angular-client',
'https://lkarow.github.io/myFlix-Angular-client/',
'http://localhost:1234',
];
app.use(
cors({
origin: (origin, callback) => {
if (!origin) return callback(null, true);
if (allowedOrigins.indexOf(origin) === -1) {
// If a specific origin isn’t found on the list of allowed origins
let message = `The CORS policy for this application doesn't allow access from origin ${origin}`;
return callback(new Error(message), false);
}
return callback(null, true);
},
})
);
require('./auth')(app);
const passport = require('passport');
require('./passport');
const { check, validationResult } = require('express-validator');
// create a write stream in log.txt
const accessLogStream = fs.createWriteStream('/tmp/log.txt', {
flags: 'a',
});
// logger setup
app.use(morgan('common', { stream: accessLogStream }));
/**
* Get data of all movies
* Endpoint: /movies
* HTTP method: GET
* @name getAllMovies
* @returns JSON object holding data of all movies
* @requires passport
*/
app.get(
'/movies',
passport.authenticate('jwt', { session: false }),
(req, res) => {
Movies.find()
.then((movies) => {
res.status(201).json(movies);
})
.catch((err) => {
console.error(err);
res.status(500).send(`Error: ${err}`);
});
}
);
/**
* Get data of a single movie
* Endpoint: /movies/[movie ID]
* HTTP method: GET
* @name getMovie
* @returns JSON object holding data about a movie containing description, genre, director, image URL
* @requires passport
*/
app.get(
'/movies/:MovieTitle',
passport.authenticate('jwt', { session: false }),
(req, res) => {
Movies.findOne({ Title: req.params.MovieTitle })
.then((movie) => {
res.status(201).json(movie);
})
.catch((err) => {
console.error(err);
res.status(500).send(`Error: ${err}`);
});
}
);
/**
* Get data of a genre
* Endpoint: /genres/[genre name]
* HTTP method: GET
* @name getGenre
* @returns JSON object holding data about a genre
* @requires passport
*/
app.get(
'/genres/:GenreName',
passport.authenticate('jwt', { session: false }),
(req, res) => {
Movies.findOne({ 'Genre.Name': req.params.GenreName })
.then((movie) => {
if (movie) {
res.status(201).json(movie.Genre);
} else {
res.status(400).send(`Genre was not found.`);
}
})
.catch((err) => {
console.error(err);
res.status(500).send(`Error: ${err}`);
});
}
);
/**
* Get data of a director
* Endpoint: /directors/[name]
* HTTP method: GET
* @name getDirector
* @returns JSON object holding data about a director including bio, birth year, death year
* @requires passport
*/
app.get(
'/directors/:DirectorName',
passport.authenticate('jwt', { session: false }),
(req, res) => {
Movies.findOne({ 'Director.Name': req.params.DirectorName })
.then((movie) => {
if (movie) {
res.status(201).json(movie.Director);
} else {
res.status(400).send(`Director was not found.`);
}
})
.catch((err) => {
console.error(err);
res.status(500).send(`Error: ${err}`);
});
}
);
/**
* Get data of a single user
* Endpoint: /users/[username]
* HTTP method: GET
* @name getUser
* @returns JSON object holding the data about the user
* @requires passport
*/
app.get(
'/users/:Username',
passport.authenticate('jwt', { session: false }),
(req, res) => {
Users.findOne({ Username: req.params.Username })
.populate('FavoriteMovies', 'Title')
.then((user) => {
res.status(201).json(user);
})
.catch((err) => {
console.error(err);
res.status(500).send(`Error: ${err}`);
});
}
);
/**
* Add new user
* Endpoint: /users
* HTTP method: POST
* Request body data format: JSON object holding data about the new user including username and mail
* Expect JSON in this format:
* {
* ID: Integer,
* Username: String, (required)
* Password: String, (required)
* Email: String, (required)
* Birthday: Date
* }
* @get addUser
* @returns JSON object holding data about the new user including ID, username and mail
*/
app.post(
'/users',
// validation logic here for request
[
check('Username', 'Username is required').isLength({ min: 5 }),
check(
'Username',
'Username contains non alphanumeric characters - not allowed.'
).isAlphanumeric(),
check('Password', 'Password is required').not().isEmpty(),
check('Email', 'Email does not appear to be valid').isEmail(),
],
(req, res) => {
// check validation object for errors
let errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(422).json({ errors: errors.array() });
}
let hashedPassword = Users.hashPassword(req.body.Password);
Users.findOne({ Username: req.body.Username })
.then((user) => {
if (user) {
return res.status(400).send(`${req.body.Username} already exists.`);
} else {
Users.create({
Username: req.body.Username,
Password: hashedPassword,
Email: req.body.Email,
Birthday: req.body.Birthday,
})
.then((user) => {
res.status(201).json(user);
})
.catch((err) => {
console.error(err);
res.status(500).send(`Error: ${err}`);
});
}
})
.catch((err) => {
console.error(err);
res.statuts(500).send(`Error: ${err}`);
});
}
);
/**
* Update user data
* Endpoint: /users/[username]
* HTTP method: PUT
* Request body data format: JSON object with the new user infos
* Expect JSON in this format:
* {
* Username: String, (required)
* Password: String, (required)
* Email: String, (required)
* Birthday: Date
* }
* @name updateUser
* @returns JSON object holding the data about the new user
* @requires passport
*/
app.put(
'/users/:Username',
passport.authenticate('jwt', { session: false }),
(req, res) => {
let hashedPassword = Users.hashPassword(req.body.Password);
Users.findOneAndUpdate(
{ Username: req.params.Username },
{
$set: {
Username: req.body.Username,
Password: hashedPassword,
Email: req.body.Email,
Birthday: req.body.Birthday,
},
},
{ new: true } // line makes sure that the update document is returned
)
.then((updateUser) => {
res.status(201).json(updateUser);
})
.catch((err) => {
console.error(err);
res.status(500).send(`Error: ${err}`);
});
}
);
/**
* Add movie to favorite list of user
* Endpoint: /users/[username]/movies/[movie ID]
* HTTP method: PUT
* @name addMovieToFavorites
* @returns JSON object holding the new data about the user
* @requires passport
*/
app.put(
'/users/:Username/movies/:MovieId',
passport.authenticate('jwt', { session: false }),
(req, res) => {
Users.findOneAndUpdate(
{ Username: req.params.Username },
{ $addToSet: { FavoriteMovies: req.params.MovieId } },
{ new: true } // line makes sure that the update document is returned
)
.populate('FavoriteMovies', 'Title')
.then((updateUser) => {
res.status(201).json(updateUser);
})
.catch((err) => {
console.error(err);
res.status(500).send(`Error: ${err}`);
});
}
);
/**
* Remove movie from favorite list of user
* Endpoint: /users/[username]/movies/[movie ID]
* HTTP method: DELETE
* @name removeMovieFromFavorites
* @returns JSON object holding the data about the user without the deleted movie
* @requires passport
*/
app.delete(
'/users/:Username/movies/:MovieId',
passport.authenticate('jwt', { session: false }),
(req, res) => {
Users.findOneAndUpdate(
{ Username: req.params.Username },
{ $pull: { FavoriteMovies: req.params.MovieId } },
{ new: true } // line makes sure that the update document is returned
)
.populate('FavoriteMovies', 'Title')
.then((updateUser) => {
res.status(201).json(updateUser);
})
.catch((err) => {
console.error(err);
res.status(500).send(`Error: ${err}`);
});
}
);
/**
* Delete user
* Endpoint: /users/[username]
* HTTP method: DELETE
* @name deleteUser
* @returns {string} text message
* @requires passport
*/
app.delete(
'/users/:Username',
passport.authenticate('jwt', { session: false }),
(req, res) => {
Users.findOneAndRemove({ Username: req.params.Username })
.then((user) => {
if (!user) {
res.status(400).send(`${req.params.Username} was not found.`);
} else {
res.status(200).send(`${req.params.Username} was deleted.`);
}
})
.catch((err) => {
console.error(err);
res.status(500).send(`Error: ${err}`);
});
}
);
app.get('/', (req, res) => {
res.send('Welcome to the movie club!');
});
app.use(express.static('public'));
// error handling
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).send('Something broke!');
});
// listen for requests
const port = process.env.PORT || 8080;
app.listen(port, () => console.log('Listening on Port ' + port));
module.exports = app;