1
+ /**
2
+ * @fileoverview Module imports and initial setup for the movie-api application.
3
+ * This file sets up the express application, configures middleware, establishes database connections,
4
+ * and defines routes for handling HTTP requests.
5
+ */
6
+
1
7
// Load necessary modules
2
8
const express = require ( 'express' ) ;
3
9
const bodyParser = require ( 'body-parser' ) ;
@@ -9,12 +15,19 @@ const Models = require('./models.js');
9
15
const { check, validationResult } = require ( 'express-validator' ) ;
10
16
require ( "dotenv" ) . config ( ) ;
11
17
12
- // Set up express and body-parser
18
+ /**
19
+ * @description Set up express and body-parser.
20
+ * This section configures the express application and sets up middleware
21
+ * for parsing JSON and URL-encoded data.
22
+ */
13
23
const app = express ( ) ;
14
24
app . use ( bodyParser . json ( ) ) ;
15
25
app . use ( bodyParser . urlencoded ( { extended : true } ) ) ;
16
26
17
- // Set up CORS
27
+ /**
28
+ * @description CORS setup to allow cross-origin requests.
29
+ * Specifies which domains are allowed to access the API.
30
+ */
18
31
const cors = require ( 'cors' ) ;
19
32
let allowedOrigins = [
20
33
'http://localhost:8080' ,
@@ -24,7 +37,6 @@ let allowedOrigins = [
24
37
'https://myflickpick.netlify.app' ,
25
38
'https://codestun.github.io'
26
39
] ;
27
-
28
40
app . use ( cors ( {
29
41
origin : ( origin , callback ) => {
30
42
if ( ! origin ) return callback ( null , true ) ;
@@ -36,17 +48,26 @@ app.use(cors({
36
48
}
37
49
} ) ) ;
38
50
39
- // Import "auth.js" file
51
+ /**
52
+ * @description Import and setup authentication module.
53
+ * Integrates Passport for user authentication within the application.
54
+ */
40
55
let auth = require ( './auth' ) ( app ) ;
41
56
42
57
// Require the Passport module and import the "passport.js" file
43
58
const passport = require ( 'passport' ) ;
44
59
require ( './passport' ) ;
45
60
46
- // Set up Morgan for logging functionality
61
+ /**
62
+ * @description Morgan logging setup.
63
+ * Integrates Morgan for logging HTTP request data for debugging and monitoring.
64
+ */
47
65
app . use ( morgan ( 'combined' ) ) ;
48
66
49
- // Connect to the MongoDB database using the connection URI from environment variable
67
+ /**
68
+ * @description Connects to the MongoDB database using Mongoose.
69
+ * The connection URI is retrieved from environment variables.
70
+ */
50
71
mongoose . connect ( process . env . CONNECTION_URI , { useNewUrlParser : true , useUnifiedTopology : true } ) ;
51
72
52
73
// Connect to the MongoDB database using the local connection URI
@@ -59,7 +80,13 @@ const Movies = Models.Movie;
59
80
const Users = Models . User ;
60
81
61
82
/* ------ Routes for Movies -------- */
62
- // Return JSON object containing data about all movies
83
+
84
+ /**
85
+ * @route GET /movies
86
+ * @description Returns a JSON object containing data about all movies.
87
+ * @access Private
88
+ * @returns {Object[] } movies - List of all movies.
89
+ */
63
90
app . get ( '/movies' , passport . authenticate ( 'jwt' , { session : false } ) , ( req , res ) => {
64
91
Movies . find ( )
65
92
. then ( ( movies ) => {
@@ -71,7 +98,13 @@ app.get('/movies', passport.authenticate('jwt', { session: false }), (req, res)
71
98
} ) ;
72
99
} ) ;
73
100
74
- // Return JSON object containing data about a single movie by title
101
+ /**
102
+ * @route GET /movies/:Title
103
+ * @description Returns data about a single movie by title.
104
+ * @access Private
105
+ * @param {string } Title - Title of the movie.
106
+ * @returns {Object } movie - Data about the movie.
107
+ */
75
108
app . get ( '/movies/:Title' , passport . authenticate ( 'jwt' , { session : false } ) , ( req , res ) => {
76
109
Movies . findOne ( { Title : req . params . Title } )
77
110
. then ( ( movie ) => {
@@ -83,7 +116,13 @@ app.get('/movies/:Title', passport.authenticate('jwt', { session: false }), (req
83
116
} ) ;
84
117
} ) ;
85
118
86
- // Return data about a genre (description) by name/title (e.g., "Rock")
119
+ /**
120
+ * @route GET /genres/:Name
121
+ * @description Returns data about a specific genre by name.
122
+ * @access Private
123
+ * @param {string } Name - Name of the genre.
124
+ * @returns {Object } genre - Description of the genre.
125
+ */
87
126
app . get ( '/genres/:Name' , passport . authenticate ( 'jwt' , { session : false } ) , ( req , res ) => {
88
127
Movies . findOne ( { "Genre.Name" : req . params . Name } )
89
128
. then ( ( movie ) => {
@@ -99,7 +138,13 @@ app.get('/genres/:Name', passport.authenticate('jwt', { session: false }), (req,
99
138
} ) ;
100
139
} ) ;
101
140
102
- // Return data about a director by name
141
+ /**
142
+ * @route GET /directors/:Name
143
+ * @description Returns data about a director by name.
144
+ * @access Private
145
+ * @param {string } Name - Name of the director.
146
+ * @returns {Object } director - Data about the director.
147
+ */
103
148
app . get ( '/directors/:Name' , passport . authenticate ( 'jwt' , { session : false } ) , ( req , res ) => {
104
149
Movies . findOne ( { "Director.Name" : req . params . Name } )
105
150
. then ( ( movie ) => {
@@ -116,7 +161,13 @@ app.get('/directors/:Name', passport.authenticate('jwt', { session: false }), (r
116
161
} ) ;
117
162
118
163
/* ------ Routes for Users -------- */
119
- // Return JSON object containing data about all users
164
+
165
+ /**
166
+ * @route GET /users
167
+ * @description Returns a list of all users.
168
+ * @access Private
169
+ * @returns {Object[] } users - List of all users.
170
+ */
120
171
app . get ( '/users' , passport . authenticate ( 'jwt' , { session : false } ) , ( req , res ) => {
121
172
Users . find ( )
122
173
. then ( ( users ) => {
@@ -128,7 +179,13 @@ app.get('/users', passport.authenticate('jwt', { session: false }), (req, res) =
128
179
} ) ;
129
180
} ) ;
130
181
131
- // Return JSON object containing data about a single user by name
182
+ /**
183
+ * @route GET /users/:Name
184
+ * @description Returns data about a single user by name.
185
+ * @access Private
186
+ * @param {string } Name - Name of the user.
187
+ * @returns {Object } user - Data about the user.
188
+ */
132
189
app . get ( '/users/:Name' , passport . authenticate ( 'jwt' , { session : false } ) , ( req , res ) => {
133
190
Users . findOne ( { Name : req . params . Name } )
134
191
. then ( ( user ) => {
@@ -140,7 +197,16 @@ app.get('/users/:Name', passport.authenticate('jwt', { session: false }), (req,
140
197
} ) ;
141
198
} ) ;
142
199
143
- // Allow new users to register
200
+ /**
201
+ * @route POST /users
202
+ * @description Allows new users to register.
203
+ * @access Public
204
+ * @param {string } Name - User's name.
205
+ * @param {string } Email - User's email.
206
+ * @param {string } Password - User's password.
207
+ * @param {Date } Birthday - User's birthday.
208
+ * @returns {Object } user - The registered user object.
209
+ */
144
210
app . post ( '/users' , [
145
211
check ( 'Name' , 'Name is required' ) . notEmpty ( ) ,
146
212
check ( 'Email' , 'Email does not appear to be valid' ) . isEmail ( ) ,
@@ -184,7 +250,16 @@ app.post('/users', [
184
250
} ) ;
185
251
186
252
187
- // Allow users to update their user info
253
+ /**
254
+ * @route PUT /users/:Name
255
+ * @description Allows users to update their information.
256
+ * @access Private
257
+ * @param {string } Name - User's updated name.
258
+ * @param {string } Email - User's updated email.
259
+ * @param {string } Password - User's updated password.
260
+ * @param {Date } Birthday - User's updated birthday.
261
+ * @returns {Object } updatedUser - The updated user object.
262
+ */
188
263
app . put ( '/users/:Name' , [
189
264
check ( 'Name' , 'Name is required' ) . notEmpty ( ) ,
190
265
check ( 'Email' , 'Email does not appear to be valid' ) . isEmail ( ) ,
@@ -221,7 +296,14 @@ app.put('/users/:Name', [
221
296
} ) ;
222
297
} ) ;
223
298
224
- // Allow users to add a movie to their list of favorites
299
+ /**
300
+ * @route POST /users/:Name/movies/:MovieID
301
+ * @description Allows users to add a movie to their list of favorites.
302
+ * @access Private
303
+ * @param {string } Name - User's name.
304
+ * @param {string } MovieID - ID of the movie to add to favorites.
305
+ * @returns {Object } updatedUser - The user object with updated favorites list.
306
+ */
225
307
app . post ( '/users/:Name/movies/:MovieID' , passport . authenticate ( 'jwt' , { session : false } ) , ( req , res ) => {
226
308
Users . findOneAndUpdate (
227
309
{ Name : req . params . Name } ,
@@ -236,7 +318,14 @@ app.post('/users/:Name/movies/:MovieID', passport.authenticate('jwt', { session:
236
318
} ) ;
237
319
} ) ;
238
320
239
- // Allow users to remove a movie from their list of favorites
321
+ /**
322
+ * @route DELETE /users/:Name/movies/:MovieID
323
+ * @description Allows users to remove a movie from their list of favorites.
324
+ * @access Private
325
+ * @param {string } Name - User's name.
326
+ * @param {string } MovieID - ID of the movie to remove from favorites.
327
+ * @returns {Object } updatedUser - The user object with updated favorites list.
328
+ */
240
329
app . delete ( '/users/:Name/movies/:MovieID' , passport . authenticate ( 'jwt' , { session : false } ) , ( req , res ) => {
241
330
Users . findOneAndUpdate (
242
331
{ Name : req . params . Name } ,
@@ -251,7 +340,13 @@ app.delete('/users/:Name/movies/:MovieID', passport.authenticate('jwt', { sessio
251
340
} ) ;
252
341
} ) ;
253
342
254
- // Allow existing users to deregister
343
+ /**
344
+ * @route DELETE /users/:Name
345
+ * @description Allows existing users to deregister.
346
+ * @access Private
347
+ * @param {string } Name - Name of the user to deregister.
348
+ * @returns {string } message - Confirmation message of user deletion.
349
+ */
255
350
app . delete ( '/users/:Name' , passport . authenticate ( 'jwt' , { session : false } ) , ( req , res ) => {
256
351
Users . findOneAndRemove ( { Name : req . params . Name } )
257
352
. then ( ( user ) => {
@@ -268,17 +363,31 @@ app.delete('/users/:Name', passport.authenticate('jwt', { session: false }), (re
268
363
} ) ;
269
364
270
365
/* ------ Other Routes -------- */
271
- // Default text response
366
+ /**
367
+ * @route GET /
368
+ * @description Default route to welcome users.
369
+ * @access Public
370
+ * @returns {string } message - Welcome message.
371
+ */
272
372
app . get ( '/' , ( req , res ) => {
273
- res . send ( 'Welcome to flickPick !' ) ;
373
+ res . send ( 'Welcome to FlickPick !' ) ;
274
374
} ) ;
275
375
276
- // Documentation endpoint
376
+ /**
377
+ * @route GET /documentation
378
+ * @description Serves the API documentation file.
379
+ * @access Public
380
+ * @returns {file } - Sends the documentation HTML file.
381
+ */
277
382
app . get ( '/documentation' , ( req , res ) => {
278
383
res . sendFile ( path . join ( __dirname , 'public' , 'documentation.html' ) ) ;
279
384
} ) ;
280
385
281
- // Error-handling middleware
386
+ /**
387
+ * @description Error handling middleware.
388
+ * Handles any errors that occur in the routing and request handling process.
389
+ */
390
+
282
391
app . use ( ( err , req , res , next ) => {
283
392
// Log error to the terminal
284
393
console . error ( err ) ;
@@ -287,7 +396,10 @@ app.use((err, req, res, next) => {
287
396
res . status ( 500 ) . send ( 'Internal Server Error' ) ;
288
397
} ) ;
289
398
290
- // Listen for connections
399
+ /**
400
+ * @description Initializes and starts the server on a specified port.
401
+ * The port number is obtained from environment variables or defaults to 8080.
402
+ */
291
403
const port = process . env . PORT || 8080 ;
292
404
app . listen ( port , '0.0.0.0' , ( ) => {
293
405
console . log ( 'Listening on Port ' + port ) ;
0 commit comments