-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
417 lines (388 loc) · 11 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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/**
* user registration object
* @typedef {Object} CreateUserPayload
* @property {string} userName - user name
* @property {string} password - password
* @property {string} email - email
* @property {Date} birthday - birthday
*/
const express = require("express"),
bodyParser = require("body-parser"),
morgan = require("morgan"),
uuid = require("uuid"),
mongoose = require("mongoose"),
Models = require("./models.js"),
Mongo = require("mongodb"),
passport = require("passport");
// For input validation
const { check, validationResult } = require("express-validator");
const { authProvider, ensureSameUser } = require("./auth");
require("./passport");
const Movies = Models.Movies;
const Users = Models.Users;
mongoose.connect(process.env.CONNECTION_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
const app = express();
//morgan function "use"
app.use(morgan("common"));
// CORS integration
const cors = require("cors");
let allowedOrigins = [
"https://myflix-client-react-redux.netlify.app",
"https://sonam-22.github.io",
"http://localhost:8080",
"http://testsite.com",
"http://localhost:1234",
];
app.use(
cors({
origin: (origin, callback) => {
if (!origin) return callback(null, true);
if (allowedOrigins.indexOf(origin) === -1) {
// origin is not included in list of allowedOrigins
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);
},
})
);
//static file given access via express static
app.use(express.static("public"));
app.use(
bodyParser.urlencoded({
extended: true,
})
);
app.use(bodyParser.json());
//integrating auth.js file for authentication and authorization using HTTP and JWSToken
let auth = authProvider(app);
/**
* @description Creates or registers a user
* @name POST /users
* @function
* @instance
* @public
* @param {CreateUserPayload} userObject
* @returns The user object
*/
app.post(
"/users",
[
check("userName", "Username is required (min 5 characters).").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); // Create hashedPassword from given Password
// Create new user
Users.findOne({ userName: req.body.userName })
.then((existingUser) => {
if (existingUser) {
// If the same username already exists, throw an error
return res
.status(400)
.send(
"User with the Username " + req.body.userName + " already exists!"
);
} else {
// If the username is unique, create a new user with the given parameters from the request body
Users.create({
userName: req.body.userName,
password: hashedPassword,
email: req.body.email,
birthday: req.body.birthday,
favouriteMovies: (req.body.favouriteMovies || []).map(
(m) => new Mongo.ObjectId(m)
),
})
.then((createdUser) => {
res.status(201).json(createdUser);
})
.catch((err) => {
console.error(err);
res.status(500).send("Error: " + err);
});
}
})
.catch((err) => {
console.error(err);
res.status(500).send("Error: " + err);
});
}
);
/**
* @description Allow users to update their user info (find by username), expecting request body with updated info
* @name PUT /users/:userName
* @function
* @public
* @instance
* @param {CreateUserPayload} userObject
* @param {string} userName user name of the user
* @returns The user object
*/
app.put(
"/users/:userName",
ensureSameUser,
passport.authenticate("jwt", { session: false }),
(req, res) => {
const updatedUser = {
// Info from request body that can be updated
userName: req.body.userName,
email: req.body.email,
birthday: req.body.birthday,
};
if (req.body.password) {
updatedUser.password = Users.hashPassword(req.body.password);
}
Users.findOneAndUpdate(
{ userName: req.params.userName }, // Find user by existing username
{
$set: updatedUser,
},
{ new: true }
) // Return the updated document
.then((updatedUser) => {
const noPassword = updatedUser;
delete noPassword.password;
res.json(noPassword); // Return json object of updatedUser
})
.catch((err) => {
console.error(err);
res.status(500).send("Error: " + err);
});
}
);
/**
* @description Add movie to favorite
* @name POST /users/:userName/movies/:MovieID
* @function
* @public
* @instance
* @param {string} userName user name of the user
* @param {string} MovieID Id of the favorite movie
* @returns The user object
*/
app.post(
"/users/:userName/movies/:MovieID",
ensureSameUser,
passport.authenticate("jwt", { session: false }),
(req, res) => {
const movieId = new Mongo.ObjectId(req.params.MovieID);
const userName = req.params.userName;
Users.findOneAndUpdate(
{
userName: userName,
favouriteMovies: { $nin: [movieId] },
}, // Find user by username
{ $push: { favouriteMovies: movieId } }, // Add movie to the list
{ new: true }
) // Return the updated document
.then((updatedUser) => {
if (!updatedUser) {
return res
.status(409)
.send(`Favourite movie ${movieId} already exists for ${userName}`);
}
res.status(200).json(updatedUser);
// Return json object of updatedUser
})
.catch((err) => {
console.error(err);
res.status(500).send("Error: " + err);
});
}
);
/**
* @description Allow users to remove a movie from their list of favorites
* @name DELETE /users/:userName/movies/:MovieID
* @function
* @public
* @instance
* @param {string} userName user name of the user
* @param {string} MovieID Id of the favorite movie
* @returns updated user object
*/
app.delete(
"/users/:userName/movies/:MovieID",
ensureSameUser,
passport.authenticate("jwt", { session: false }),
(req, res) => {
Users.findOneAndUpdate(
{ userName: req.params.userName }, // Find user by username
{ $pull: { favouriteMovies: new Mongo.ObjectId(req.params.MovieID) } }, // Remove movie from the list
{ new: true }
) // Return the updated document
.then((updatedUser) => {
res.json(updatedUser); // Return json object of updatedUser
})
.catch((err) => {
console.error(err);
res.status(500).send("Error: " + err);
});
}
);
/**
* @description Deregisters a user
* @name DELETE /users/:userName
* @function
* @instance
* @public
* @param {string} userName user name of the user
* @returns void
*/
app.delete(
"/users/:userName",
ensureSameUser,
passport.authenticate("jwt", { session: false }),
(req, res) => {
Users.findOneAndRemove({ userName: req.params.userName }) // Find user by username
.then((users) => {
if (users) {
// If user was found, return success message, else return error
res
.status(200)
.send(
`User with the Username ${req.params.userName} was sucessfully deleted.`
);
} else {
res
.status(400)
.send(
`User with the Username ${req.params.userName} was not found.`
);
}
})
.catch((err) => {
console.error(err);
res.status(500).send("Error: " + err);
});
}
);
// Read
app.get("/", (req, res) => {
res.send("Welcome to my movie list!");
});
/**
* @description Gets list of movies
* @name GET /movies
* @function
* @public
* @instance
* @returns Array of movies
*/
app.get("/movies", (req, res) => {
Movies.find()
.then((movies) => {
res.status(200).json(movies);
})
.catch((err) => {
res.status(500).send("Error: " + err);
});
});
/**
* @description Gets list of movie by title
* @name GET /movies/:title
* @function
* @instance
* @public
* @param {string} title title of the movie
* @returns Movie object
*/
app.get(
"/movies/:title",
passport.authenticate("jwt", { session: false }),
(req, res) => {
Movies.findOne({ Title: req.params.title }) // Find the movie by title
.then((movies) => {
if (movies) {
// If movie was found, return json, else throw error
res.status(200).json(movies);
} else {
res.status(400).send("Movie not found");
}
})
.catch((err) => {
res.status(500).send("Error: " + err);
});
}
);
/**
* @description Gets data about a genre (description) by name/title
* @name GET /movies/genre/:name
* @function
* @instance
* @public
* @param {string} name name of the genre
* @returns Genre object
*/
app.get(
"/movies/genre/:name",
passport.authenticate("jwt", { session: false }),
(req, res) => {
Movies.findOne({ "Genre.Name": req.params.name }) // Find one movie with the genre by genre name
.then((movies) => {
if (movies) {
// If a movie with the genre was found, return json of genre info, else throw error
res.status(200).json(movies.Genre);
} else {
res.status(400).send("Genre not found");
}
})
.catch((err) => {
res.status(500).send("Error: " + err);
});
}
);
/**
* @description Gets data about a director (bio, birth year, death year) by name
* @name GET /movies/director/:name
* @function
* @instance
* @public
* @param {string} name name of the director
* @returns Directors object
*/
app.get(
"/movies/director/:name",
passport.authenticate("jwt", { session: false }),
(req, res) => {
Movies.findOne({ "Director.Name": req.params.name }) // Find one movie with the director by name
.then((movies) => {
if (movies) {
// If a movie with the director was found, return json of director info, else throw error
res.status(200).json(movies.Director);
} else {
res.status(400).send("Director not found");
}
})
.catch((err) => {
res.status(500).send("Error: " + err);
});
}
);
// error-handling middleware function
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, "0.0.0.0", () => {
console.log("Listening on Port " + port);
});