Skip to content

Commit 82b01c1

Browse files
committed
Add JSDoc to API files; generate docs
1 parent a99bb35 commit 82b01c1

34 files changed

+13270
-35
lines changed

auth.js

+15-5
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,27 @@ const passport = require('passport');
66

77
require('./passport'); // Your local passport file
88

9-
// Function to generate JWT token
9+
/**
10+
* Generates a JWT token for a user.
11+
* @param {Object} user - The user object for whom the token is being generated.
12+
* @returns {string} JWT token.
13+
*/
1014
let generateJWTToken = (user) => {
1115
return jwt.sign(user, jwtSecret, {
12-
subject: user.Name, // This is the Name you’re encoding in the JWT
13-
expiresIn: '7d', // This specifies that the token will expire in 7 days
14-
algorithm: 'HS256' // This is the algorithm used to "sign" or encode the values of the JWT
16+
subject: user.Name, // The username encoded in the JWT
17+
expiresIn: '7d', // Token expiration time
18+
algorithm: 'HS256' // Algorithm used to encode the JWT
1519
});
1620
};
1721

18-
/* ------ Post login. -------- */
22+
/* POST login route */
1923
module.exports = (router) => {
24+
/**
25+
* @route POST /login
26+
* @description Authenticates a user and returns a JWT token upon successful authentication.
27+
* @param {Object} req - The request object containing user credentials.
28+
* @param {Object} res - The response object.
29+
*/
2030
router.post('/login', (req, res) => {
2131
passport.authenticate('local', { session: false }, (error, user, info) => {
2232
if (error || !user) {

index.js

+134-22
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
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+
17
// Load necessary modules
28
const express = require('express');
39
const bodyParser = require('body-parser');
@@ -9,12 +15,19 @@ const Models = require('./models.js');
915
const { check, validationResult } = require('express-validator');
1016
require("dotenv").config();
1117

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+
*/
1323
const app = express();
1424
app.use(bodyParser.json());
1525
app.use(bodyParser.urlencoded({ extended: true }));
1626

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+
*/
1831
const cors = require('cors');
1932
let allowedOrigins = [
2033
'http://localhost:8080',
@@ -24,7 +37,6 @@ let allowedOrigins = [
2437
'https://myflickpick.netlify.app',
2538
'https://codestun.github.io'
2639
];
27-
2840
app.use(cors({
2941
origin: (origin, callback) => {
3042
if (!origin) return callback(null, true);
@@ -36,17 +48,26 @@ app.use(cors({
3648
}
3749
}));
3850

39-
// Import "auth.js" file
51+
/**
52+
* @description Import and setup authentication module.
53+
* Integrates Passport for user authentication within the application.
54+
*/
4055
let auth = require('./auth')(app);
4156

4257
// Require the Passport module and import the "passport.js" file
4358
const passport = require('passport');
4459
require('./passport');
4560

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+
*/
4765
app.use(morgan('combined'));
4866

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+
*/
5071
mongoose.connect(process.env.CONNECTION_URI, { useNewUrlParser: true, useUnifiedTopology: true });
5172

5273
// Connect to the MongoDB database using the local connection URI
@@ -59,7 +80,13 @@ const Movies = Models.Movie;
5980
const Users = Models.User;
6081

6182
/* ------ 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+
*/
6390
app.get('/movies', passport.authenticate('jwt', { session: false }), (req, res) => {
6491
Movies.find()
6592
.then((movies) => {
@@ -71,7 +98,13 @@ app.get('/movies', passport.authenticate('jwt', { session: false }), (req, res)
7198
});
7299
});
73100

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+
*/
75108
app.get('/movies/:Title', passport.authenticate('jwt', { session: false }), (req, res) => {
76109
Movies.findOne({ Title: req.params.Title })
77110
.then((movie) => {
@@ -83,7 +116,13 @@ app.get('/movies/:Title', passport.authenticate('jwt', { session: false }), (req
83116
});
84117
});
85118

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+
*/
87126
app.get('/genres/:Name', passport.authenticate('jwt', { session: false }), (req, res) => {
88127
Movies.findOne({ "Genre.Name": req.params.Name })
89128
.then((movie) => {
@@ -99,7 +138,13 @@ app.get('/genres/:Name', passport.authenticate('jwt', { session: false }), (req,
99138
});
100139
});
101140

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+
*/
103148
app.get('/directors/:Name', passport.authenticate('jwt', { session: false }), (req, res) => {
104149
Movies.findOne({ "Director.Name": req.params.Name })
105150
.then((movie) => {
@@ -116,7 +161,13 @@ app.get('/directors/:Name', passport.authenticate('jwt', { session: false }), (r
116161
});
117162

118163
/* ------ 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+
*/
120171
app.get('/users', passport.authenticate('jwt', { session: false }), (req, res) => {
121172
Users.find()
122173
.then((users) => {
@@ -128,7 +179,13 @@ app.get('/users', passport.authenticate('jwt', { session: false }), (req, res) =
128179
});
129180
});
130181

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+
*/
132189
app.get('/users/:Name', passport.authenticate('jwt', { session: false }), (req, res) => {
133190
Users.findOne({ Name: req.params.Name })
134191
.then((user) => {
@@ -140,7 +197,16 @@ app.get('/users/:Name', passport.authenticate('jwt', { session: false }), (req,
140197
});
141198
});
142199

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+
*/
144210
app.post('/users', [
145211
check('Name', 'Name is required').notEmpty(),
146212
check('Email', 'Email does not appear to be valid').isEmail(),
@@ -184,7 +250,16 @@ app.post('/users', [
184250
});
185251

186252

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+
*/
188263
app.put('/users/:Name', [
189264
check('Name', 'Name is required').notEmpty(),
190265
check('Email', 'Email does not appear to be valid').isEmail(),
@@ -221,7 +296,14 @@ app.put('/users/:Name', [
221296
});
222297
});
223298

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+
*/
225307
app.post('/users/:Name/movies/:MovieID', passport.authenticate('jwt', { session: false }), (req, res) => {
226308
Users.findOneAndUpdate(
227309
{ Name: req.params.Name },
@@ -236,7 +318,14 @@ app.post('/users/:Name/movies/:MovieID', passport.authenticate('jwt', { session:
236318
});
237319
});
238320

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+
*/
240329
app.delete('/users/:Name/movies/:MovieID', passport.authenticate('jwt', { session: false }), (req, res) => {
241330
Users.findOneAndUpdate(
242331
{ Name: req.params.Name },
@@ -251,7 +340,13 @@ app.delete('/users/:Name/movies/:MovieID', passport.authenticate('jwt', { sessio
251340
});
252341
});
253342

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+
*/
255350
app.delete('/users/:Name', passport.authenticate('jwt', { session: false }), (req, res) => {
256351
Users.findOneAndRemove({ Name: req.params.Name })
257352
.then((user) => {
@@ -268,17 +363,31 @@ app.delete('/users/:Name', passport.authenticate('jwt', { session: false }), (re
268363
});
269364

270365
/* ------ 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+
*/
272372
app.get('/', (req, res) => {
273-
res.send('Welcome to flickPick!');
373+
res.send('Welcome to FlickPick!');
274374
});
275375

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+
*/
277382
app.get('/documentation', (req, res) => {
278383
res.sendFile(path.join(__dirname, 'public', 'documentation.html'));
279384
});
280385

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+
282391
app.use((err, req, res, next) => {
283392
// Log error to the terminal
284393
console.error(err);
@@ -287,7 +396,10 @@ app.use((err, req, res, next) => {
287396
res.status(500).send('Internal Server Error');
288397
});
289398

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+
*/
291403
const port = process.env.PORT || 8080;
292404
app.listen(port, '0.0.0.0', () => {
293405
console.log('Listening on Port ' + port);

0 commit comments

Comments
 (0)