-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
309 lines (280 loc) · 10.3 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
const express = require('express');
const morgan = require('morgan');
const bodyParser = require('body-parser');
const methodOverride = require('method-override');
const mongoose = require('mongoose');
const Models = require('./models.js');
const cors = require('cors');
const { check, validationResult } = require('express-validator');
// hosting client on heroku together with api: const path = require('path');
const path = require('path');
const app = express();
//call dotenv
require('dotenv').config();
// testing connection mongodb
console.log(process.env);
// MongoDB connections
//connecting local db
/* mongoose.connect('mongodb://127.0.0.1:27017/myFlixDB', { useNewUrlParser: true, useUnifiedTopology: true });
*/
//connecting cloud mongo using heroku
mongoose.connect(process.env.MONGODB_URI,
{ useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true, })
.then(() => {
console.log("Successfully connected to MongoDB Atlas!");
})
.catch((error) => {
console.log("Unable to connect to MongoDB Atlas!");
console.error(error);
});
//for connecting to mongoDB
const Movies = Models.Movie;
const Users = Models.User;
//serving static content
app.use(express.static('public'));
// Prepare for hosting on Heroku together with api
app.use('/client', express.static(path.join(__dirname, 'client', 'dist')));
//logging using morgan
app.use(morgan('common'));
//error handling
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.use(methodOverride());
//for allowing app access from by others
const allowedOrigins = ['http://localhost:3000', 'http://localhost:8080', 'https://awesome-movie.herokuapp.com', 'https://awesome-movie.herokuapp.com/login','http://localhost:1234'];
app.use(cors({
origin: (origin, callback) => {
if(!origin) return callback(null, true);
if(allowedOrigins.indexOf(origin) === -1){
//if specified 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);
}
}));
//auth module requires body parser
const passport = require('passport');
app.use(passport.initialize())
let auth = require('./auth')(app);
require('./passport');
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).send('Error has occured!');
})
// before client
const port = process.env.PORT || 3000;
app.listen(port, '0.0.0.0', () => {
console.log(`Listening on Port ${port}`);
})
//API endpoints start
app.get('/', (req, res) => {
res.send("Welcome to my CloudFlix API !!!");
});
// hosting client on heroku together with api:
app.get('/client/*', (req, res) => {
res.sendFile(path.join(__dirname, 'client', 'dist', 'index.html'));
});
// Return a list of ALL movies to the user
//commented for frontend exercise
app.get('/movies', passport.authenticate('jwt', { session: false }), (req, res) => {
Movies.find()
.then((movies) => {
res.status(200).json(movies)
})
.catch((err) => {
console.error(err);
res.status(500).send(`Error: ${err}`);
});
});
// Return data (description, genre, director, image URL, whether it’s featured or not) about a single movie by title to the user
app.get('/movies/:Title', passport.authenticate('jwt',{ session: false }), (req, res) => {
Movies.findOne({ Title: req.params.Title})
.then((movie) => {
res.status(200).json(movie);
})
.catch((err) => {
console.error(err);
res.status(500).send(`Error: ${err}`);
});
});
// Return data about a genre (description) by name/title (e.g., “Thriller”)
app.get('/movies/Genre/:Name', passport.authenticate('jwt',{ session: false }), (req, res) => {
Movies.findOne({ "Genre.Name": req.params.Name})
.then((genre)=>{
res.status(200).json(genre);
})
.catch((err) => {
console.error(err);
res.status(500).send(`Error: ${err}`);
});
});
// Return data about a director (bio, birth year, death year) by name
app.get('/movies/Director/:Name', passport.authenticate('jwt',{ session: false }), (req, res) => {
Movies.findOne({ "Director.Name": req.params.Name})
.then((director)=>{
res.status(200).json(director);
})
.catch((err) => {
console.error(err);
res.status(500).send(`Error: ${err}`);
});
});
// GET all users (For testing sake only !!
app.get("/users", (req, res) => {
Users.find()
.then((users) => {
res.status(201).json(users);
})
.catch((err) => {
console.error(err);
res.status(500).send("Error: " + err);
});
}
);
// Allow new users to register
app.post('/users',
[
check('Username', 'Username is required').not().isEmpty(),
//check('Username', 'Username must contain atleast 5 characters').isLength({min: 5}),
check('Username', 'Username must contain alphanumeric characters').isAlphanumeric(),
check('Password', 'Password is required').not().isEmpty(),
check('Email', 'Email is not valid').isEmail(),
],
(req, res) => {
let errors = validationResult(req);
if(!errors.isEmpty()){
return res.status(422).json({errors: errors.array()});
}
const { Username, Password, Email, Birthday } = req.body;
const hashedPassword = Users.hashPassword(Password);
// Search to see if a user with the requested username already exists
Users.findOne({Username: Username})
.then((user) => {
//If the user is found, send a response that it already exists
if(user) {
return res.status(400).send(`${Username} already exists`);
}else{
Users.create({
Username,
Password: hashedPassword,
Email,
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.status(500).send(`Error: ${err}`);
});
});
// Allow users to update their user info (username)
app.put('/users/:Username',
passport.authenticate('jwt', { session: false }),
[
check('Username', 'Username is required').not().isEmpty(),
check('Username', 'Username must contain atleast 5 characters').isLength({min: 5}),
check('Username', 'Username must contain alphanumeric characters').isAlphanumeric(),
check('Password', 'Password is required').not().isEmpty(),
check('Password', 'Password must contain atleast 5 characters').isLength({min: 5}),
check('Email', 'Email is not valid').isEmail(),
],
(req, res) => {
let errors = validationResult(req);
if(!errors.isEmpty()){
return res.status(422).json({errors: errors.array()});
}
const { Username, Password, Email, Birthday } = req.body;
const hashedPassword = Users.hashPassword(Password);
Users.findOneAndUpdate(
{Username : req.params.Username},
{
$set: {
Username,
Password : hashedPassword,
Email,
Birthday
}
},
{ new: true },
(err, updatedUser) => {
if(err){
console.error(err);
res.status(500).send(`Error: ${err}`);
}
else{
res.status(200).json(updatedUser);
}
}
);
});
// Allow users to add a movie to their list of favorites (showing only a text that a movie has been added—more on this later)
app.post('/users/:Username/movies/:_id', passport.authenticate('jwt', { session: false }), (req, res) => {
const {Username, MovieID} = req.params;
Users.findOneAndUpdate(
{ Username },
{
$push: { FavoriteMovies: MovieID }
},
{ new: true },
(err, updatedUser) => {
if(err){
console.error(err);
res.status(500).send(`Error: ${err}`);
}
else{
res.status(200).json(updatedUser);
}
}
);
});
// Allow users to remove a movie from their list of favorites (showing only a text that a movie has been removed—more on this later)
app.delete('/users/:Username/movies/:_id', passport.authenticate('jwt', { session: false }), (req, res) => {
const {Username, MovieID} = req.params;
Users.findOneAndUpdate(
{ Username },
{
$pull: { FavoriteMovies: MovieID }
},
{ new: true },
(err, updatedUser) => {
if(err){
console.error(err);
res.status(500).send(`Error: ${err}`);
}
else{
res.status(200).json(updatedUser);
}
}
);
});
// Allow existing users to deregister (showing only a text that a user email has been removed—more on this later)
app.delete('/users/:Username', passport.authenticate('jwt', { session: false }), (req, res) => {
const { Username } = req.params;
Users.findOneAndRemove({Username: Username})
.then((user) => {
if(!user){
res.status(400).send(`${Username} was not found`);
}else{
res.status(200).send(`${Username} was deleted.`);
}
})
.catch((err) => {
console.error(err);
res.status(500).send(`Error: ${err}`);
});
});
// app.listen(3000, () => {
// console.log('Server started on port 3000');
//});