-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
398 lines (368 loc) · 14.5 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
/**
* @file index.js
* @description Entry point for the myFlix API. Sets up Express server, routes, and connects to MongoDB.
*/
const express = require('express'),
app = express(),
bodyParser = require('body-parser')
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// import express-validator
const { check, validationResult } = require('express-validator');
// import cors
const cors = require('cors');
//app.use(cors()); // allows all domains to access the API -- disabled for security reasons
// to allow only certain origins to access the API.
// creates a list of allowed domains within the variable allowedOrigins,
// then compares the domains of any incoming request with this list and either
// allows it (if the domain is on the list) or returns an error (if the domain isn’t on the list)
let allowedOrigins = [
'http://localhost:8080',
'http://localhost:4200',
'http://localhost:1234',
'http://testsite.com',
'https://stix2you.github.io',
'https://myflix-dhill-portfolio-site.netlify.app'
];
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);
}
}));
// import auth.js file
let auth = require('./auth')(app); // app argument ensures Express is available in the auth.js file
// require the Passport module, import passport.js below
const passport = require('passport');
require('./passport');
// Imports or "requires" the Mongoose package, and the models we defined in the models.js file
const mongoose = require('mongoose');
const Models = require('./models.js');
// so the variables 'Movies' and 'User' (etc..) will point to the MODEL NAMES we defined in models.js
const Movies = Models.Movie;
const Users = Models.User;
const Genres = Models.Genre;
const Directors = Models.Director;
const Actors = Models.Actor;
/**
* This function returns a welcome message to the user
* @route GET /
* @description - empty endpoint - starting point for the API
* @group Welcome - Welcome message
* @returns {string} Welcome message
*/
app.get('/', (req, res) => {
res.send('Welcome to myFlix Movie App!');
});
/**
* Return a list of all movies
* @route GET /movies
* @group Movies - Operations about movies
* @returns {object} 200 - An array of movies
* @returns {Error} default - Unexpected error
*/
app.get('/movies', passport.authenticate('jwt', { session: false }), // 'session: false' tells Passport not to create a server-side session
async (req, res) => { // async function allows us to use the 'await' keyword, req and res are objects representing the HTTP request and response
await Movies.find()
.then((movies) => {
res.status(201).json(movies);
})
.catch((err) => {
console.error(err);
res.status(500).send('Error: ' + err);
});
});
/**
* Return data about a single movie by title
* @route GET /movies/:Title
* @group Movies - Operations about movies
* @param {string} Title.path.required - Movie title
* @returns {object} 200 - A movie object
* @returns {Error} default - Unexpected error
*/
app.get('/movies/:Title', passport.authenticate('jwt', { session: false }),
async (req, res) => {
await Movies.findOne({ Title: req.params.Title })
.then((movies) => {
res.json(movies);
})
.catch((err) => {
console.error(err);
res.status(500).send('Error: ' + err);
});
});
/**
* Return data about a genre by name
* @route GET /genres/:name
* @group Genres - Operations about genres
* @param {string} name.path.required - Genre name
* @returns {object} 200 - A genre object
* @returns {Error} default - Unexpected error
*/
app.get('/genres/:name', passport.authenticate('jwt', { session: false }),
async (req, res) => {
await Genres.findOne({ name: req.params.name })
.then((genres) => {
res.json(genres);
})
.catch((err) => {
console.error(err);
res.status(500).send('Error: ' + err);
});
});
/**
* Return data about a director by name
* @route GET /directors/:name
* @group Directors - Operations about directors
* @param {string} name.path.required - Director name
* @returns {object} 200 - A director object
* @returns {Error} default - Unexpected error
*/
app.get('/directors/:name', passport.authenticate('jwt', { session: false }),
async (req, res) => {
await Directors.findOne({ name: req.params.name })
.then((name) => {
res.json(name);
})
.catch((err) => {
console.error(err);
res.status(500).send('Error: ' + err);
});
});
/**
* Add a user
* @route POST /users
* @group Users - Operations about users
* @param {string} username.body.required - Username
* @param {string} password.body.required - Password
* @param {string} email.body.required - Email
* @param {string} birthday.body - Birthday
* @returns {object} 201 - User created
* @returns {Error} default - Unexpected error
*/
app.post('/users', [
check('username', 'Username is required').isLength({ min: 5 }), // checks if the username is at least 5 characters long
check('username', 'Username contains non alphanumeric characters - not allowed.').isAlphanumeric(), // checks if the username contains only alphanumeric characters
check('password', 'Password is required').not().isEmpty(), // checks if the password is not empty
check('email', 'Email does not appear to be valid').isEmail() // checks if the email is valid
], async (req, res) => {
// check the validation object for errors
let errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(422).json({ errors: errors.array() }); // if there are errors, return a 422 response with a JSON object containing the error messages
}
// Hash the password
let hashedPassword = Users.hashPassword(req.body.password); // hashPassword is a method defined in the models.js file, req.body.Password is the password passed from the user
// check to see if the user already exists:
await Users.findOne({ username: req.body.username }) // uses the findOne method to look for a user with the same username, req.body.username is the username passed from the user
.then((user) => {
if (user) {
return res.status(400).send(req.body.username + 'already exists'); // If the user is found, send a response that it already exists and exit the function
} else {
// Create the user. req.body is the information passed from the user. Creates a new USER DOCUMENT
Users
.create({ // create method is used to create a new document in the database
username: req.body.username,
password: hashedPassword, // hashed password !!!
email: req.body.email,
birthday: req.body.birthday
})
// Now takes the document just added and sends it back to the user along with a status message.
// Note: CALLBACK function is within the promise, not in the main function.
.then((user) => { res.status(201).json(user) })
.catch((error) => {
console.error(error);
res.status(500).send('Error: ' + error);
})
}
})
// .catch function will catch any problems Mongoose encounters while running the create command.
.catch((error) => {
console.error(error);
res.status(500).send('Error: ' + error);
});
});
/**
* Update user information
* @route PUT /users/:Username
* @group Users - Operations about users
* @param {string} Username.path.required - Username
* @param {string} username.body - New username
* @param {string} password.body - New password
* @param {string} email.body - New email
* @param {string} birthday.body - New birthday
* @returns {object} 200 - Updated user object
* @returns {Error} default - Unexpected error
*/
app.put('/users/:Username', [
// Include validation checks for fields only if they need to be validated
check('username')
.optional({ checkFalsy: true })
.isAlphanumeric().withMessage('Username contains non alphanumeric characters - not allowed.'),
check('email')
.optional({ checkFalsy: true })
.isEmail().withMessage('Email does not appear to be valid'),
], passport.authenticate('jwt', { session: false }),
async (req, res) => {
// check the validation object for errors
let errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(422).json({ errors: errors.array() });
}
// Construct update object based on provided fields
let updateObject = {};
if (req.body.username) updateObject.username = req.body.username;
if (req.body.password) updateObject.password = Users.hashPassword(req.body.password);
if (req.body.email) updateObject.email = req.body.email;
if (req.body.birthday) updateObject.birthday = req.body.birthday;
// Proceed with update only if there's at least one field to update
if (Object.keys(updateObject).length > 0) {
try {
const updatedUser = await Users.findOneAndUpdate(
{ username: req.params.Username },
{ $set: updateObject },
{ new: true, runValidators: true } // Ensure new document is returned and validators run
);
res.json(updatedUser);
} catch (err) {
console.error(err);
res.status(500).send('Error: ' + err);
}
} else {
res.status(400).send('No update fields provided');
}
});
/**
* Add a movie to a user's list of favorites
* @route POST /users/:username/movies/:_id
* @group Users - Operations about users
* @param {string} username.path.required - Username
* @param {string} _id.path.required - Movie ID
* @returns {object} 200 - Updated user object
* @returns {Error} default - Unexpected error
*/
app.post('/users/:username/movies/:_id', passport.authenticate('jwt', { session: false }),
async (req, res) => {
await Users.findOneAndUpdate({ username: req.params.username }, {
$addToSet: { favorite_movies: req.params._id }
},
{ new: true }) // This line makes sure that the updated document is returned
.then((updatedUser) => {
res.json(updatedUser);
})
.catch((err) => {
console.error(err);
res.status(500).send('Error: ' + err);
});
});
/**
* Delete a movie from a user's list of favorites
* @route DELETE /users/:username/movies/:_id
* @group Users - Operations about users
* @param {string} username.path.required - Username
* @param {string} _id.path.required - Movie ID
* @returns {string} 200 - Confirmation message
* @returns {Error} default - Unexpected error
*/
app.delete('/users/:username/movies/:_id', passport.authenticate('jwt', { session: false }),
async (req, res) => {
await Users.findOneAndUpdate({ username: req.params.username }, {
$pull: { favorite_movies: req.params._id }
},
{ new: true }) // This line makes sure that the updated document is returned
.then((updatedUser) => {
res.send(req.params._id + ' was deleted.');
// res.json(updatedUser);
})
.catch((err) => {
console.error(err);
res.status(500).send('Error: ' + err);
});
});
/**
* Delete a user by username
* @route DELETE /users/:username
* @group Users - Operations about users
* @param {string} username.path.required - Username
* @returns {string} 200 - Confirmation message
* @returns {Error} default - Unexpected error
*/
app.delete('/users/:username', passport.authenticate('jwt', { session: false }),
async (req, res) => {
await Users.findOneAndDelete({ 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);
});
});
/**
* Get all users
* @route GET /users
* @group Users - Operations about users
* @returns {object} 200 - An array of users
* @returns {Error} default - Unexpected error
*/
app.get('/users', passport.authenticate('jwt', { session: false }),
async (req, res) => {
await Users.find()
.then((users) => {
res.status(201).json(users);
})
.catch((err) => {
console.error(err);
res.status(500).send('Error: ' + err);
});
});
/**
* Get a user by username
* @route GET /users/:username
* @group Users - Operations about users
* @param {string} username.path.required - Username
* @returns {object} 200 - A user object
* @returns {Error} default - Unexpected error
*/
app.get('/users/:username', passport.authenticate('jwt', { session: false }),
async (req, res) => {
await Users.findOne({ username: req.params.username })
.then((users) => {
res.json(users);
})
.catch((err) => {
console.error(err);
res.status(500).send('Error: ' + err);
});
});
// START SERVER
mongoose.connect(process.env.CONNECTION_URI, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => {
console.log('Connected to MongoDB');
const port = process.env.PORT || 8080; // sets the port to 8080 if there is no PORT environment variable
app.listen(port, '0.0.0.0', () => {
console.log('Listening on Port ' + port);
});
})
.catch(err => {
console.error('Error connecting to MongoDB:', err);
});
// mongoose.connect('mongodb://127.0.0.1:27017/myFlix')
// .then(() => {
// console.log('Connected to MongoDB');
// const port = process.env.PORT || 8080;
// app.listen(port, '0.0.0.0', () => {
// console.log('Listening on Port ' + port);
// });
// })
// .catch(err => {
// console.error('Error connecting to MongoDB:', err);
// });