-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
436 lines (370 loc) · 13.7 KB
/
server.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
'use strict'
const env = process.env.NODE_ENV
const USE_TEST_USER = env !== 'production' && process.env.TEST_USER_ON // option to turn on the test user.
const TEST_USER_ID = '5fb8b011b864666580b4efe3' // the id of our test user (you will have to replace it with a test user that you made). can also put this into a separate configutation file
const TEST_USER_EMAIL = '[email protected]'
const express = require("express");
const path = require('path')
const app = express();
// enable CORS if in development, for React local development server to connect to the web server.
const cors = require('cors')
if (env !== 'production') { app.use(cors({
origin: "http://localhost:3000",
credentials: true,
})) }
// cloudinary: configure using credentials found on your Cloudinary Dashboard
// sign up for a free account here: https://cloudinary.com/users/register/free
const cloudinary = require('cloudinary');
cloudinary.config({
cloud_name: 'duzagwls0',
api_key: '956617131427852',
api_secret: 'f74CJIBdU3jK7ejn86uTtmezkOQ'
});
// multipart middleware: allows you to access uploaded file from req.file
const multipart = require('connect-multiparty');
const multipartMiddleware = multipart();
const log = console.log
const { getAllData } = require('./requests/allNeighbourhoodsData')
const { mongoose } = require("./db/mongoose");
const { ObjectID, ObjectId } = require("mongodb");
// Models
const { User } = require("./models/user");
const { Neighborhood } = require("./models/neighborhoods");
const { Review } = require("./models/review")
// Some middleware dependencies
const { authenticateUser } = require('./server-middleware/authenticateUser')
const bodyParser = require('body-parser');
const { restart } = require("nodemon");
const { response } = require("express");
// Middleware for bodyParser
app.use(bodyParser.json()) // parsing JSON body
app.use(bodyParser.urlencoded({ extended: true })); // parsing URL-encoded form data (from form POST requests)
// Express-sessions to help manage user sessions
const session = require("express-session")
const MongoStore = require('connect-mongo')
const { addEconomics } = require("./requests/neighEconomics")
const { send } = require("express/lib/response")
/*** Image API Routes below ************************************/
const { Image } = require("./models/image");
// a POST route to *create* an image
/*** USERS API ROUTES ***/
app.post('/api/users', async (req, res) => {
console.log(req.body)
const isAdmin = req.body.isAdmin ? true : false
// Create a new user
const user = new User({
email: req.body.email,
username: req.body.username,
password: req.body.password,
isAdmin: isAdmin
})
try {
const newUser = await user.save()
res.status(200).send(newUser)
} catch (error) {
// Existing user
//log(error.name)
if (error.name === 'MongoServerError' && error.code === 11000) {
return res.status(422).send({success: false})
}
//log(error);
res.status(400).send('Bad Request')
}
})
/** USER SESSIONS HANDLING **/
app.use(
session({
secret: process.env.SESSION_SECRET || "our hardcoded secret", // make a SESSION_SECRET environment variable when deploying (for example, on heroku)
resave: false,
saveUninitialized: false,
cookie: {
expires: 5000000,
httpOnly: true
},
// store the sessions on the database in production
store: MongoStore.create(
{mongoUrl: process.env.MONGODB_URI || 'mongodb+srv://team49:[email protected]/neighbourhoodlyAPI?retryWrites=true&w=majority'
})
})
)
// A route to login and create a session
app.post("/users/login", (req, res) => {
const email = req.body.email;
const password = req.body.password;
// log(email, password);
// Use the static method on the User model to find a user
// by their email and password
User.findByEmailPassword(email, password)
.then(user => {
// Add the user's id to the session.
// We can check later if this exists to ensure we are logged in.
req.session.user = user._id;
req.session.email = user.email; // we will later send the email to the browser when checking if someone is logged in through GET /check-session (we will display it on the frontend dashboard. You could however also just send a boolean flag).
req.session.username = user.username;
req.session.isAdmin = user.isAdmin;
res.send({ currentUser:{email: req.session.email, username: req.session.username}, isAdmin: user.isAdmin});
})
.catch(error => {
res.status(400).send("Invalid credentials")
});
});
app.get('/api/users', authenticateUser, async (req, res) => {
try {
if (!req.user.isAdmin) {
log("Access Denied")
return res.status(401).send("Unauthorized")
}
const users = await User.find({}, 'email username isAdmin _id')
res.send(users)
} catch(error) {
log(error)
res.status(500).send("Internal Server Error")
}
})
// Add authenticate later
app.get('/api/users/current', authenticateUser, async (req, res) => {
const sessionUser = req.user.username // coming from authenticate middleware
try {
const user = await User.findOne({username: sessionUser}, '-_id, -password -__v')
console.log(user)
res.status(200).send(user)
} catch (e) {
res.status(400).send("Bad request")
}
})
// Add authenticate later
app.get('/api/users/:id', async (req, res) => {
const id = req.params.id
try {
const user = await User.findById(id, '-_id, -password -__v')
console.log(user)
res.status(200).send(user)
} catch (e) {
res.status(400).send("Bad request")
}
})
// Add authenticateUser later
app.put("/api/users/edit", multipartMiddleware, authenticateUser, async (req, res) => {
// req.files gives us files,
// req.body gives us the text with property "name" from the form
// Use uploader.upload API to upload image to cloudinary server.
const newData = {}
const sessionUser = req.user.username // Coming from auth middleware
if (req.files.image.originalFilename.trim().length !== 0 ) {
const result = await cloudinary.uploader.upload(req.files.image.path) // req.files contains uploaded files
newData.image = {
image_id: result.public_id, // image id on cloudinary server
image_url: result.url, // image url on cloudinary server
created_at: new Date(),
}
}
// If a user updated their username
if (req.body.username) {
newData.username = req.body.username;
}
if (req.body.about) {
newData.about = req.body.about
}
if (req.body.location) {
newData.location = req.body.location
}
console.log(newData)
// If no data was added, send a bad request message
if (Object.keys(newData).length === 0) {
return res.status(400).send('Bad Request')
}
try {
console.log(sessionUser)
await User.findOneAndUpdate({username: sessionUser}, {$set: newData})
// Updating session username
if (newData.username) {
req.session.username = newData.username
}
console.log('Success')
res.status(200).send()
} catch (e) {
log(e)
res.status(500).send('Server error')
}
})
// A route to logout a user
app.get("/users/logout", (req, res) => {
// Remove the session
req.session.destroy(error => {
if (error) {
res.status(500).send(error);
} else {
res.send()
}
});
});
// A route to check if a user is logged in on the session
app.get("/users/check-session", (req, res) => {
if (env !== 'production' && USE_TEST_USER) { // test user on development environment.
req.session.user = TEST_USER_ID;
req.session.email = TEST_USER_EMAIL;
res.send({ currentUser: TEST_USER_EMAIL })
return;
}
if (req.session.user) {
log(req.session)
res.send({ currentUser: {email: req.session.email, username: req.session.username}, isAdmin: req.session.isAdmin});
} else {
res.status(401).send();
}
});
/*** NEIGHBOURHOODS API ROUTES ***/
/*
This route is adding the formatted neighbourhoods data from City of Toronto API's found in requests.
No request params should be sent to this api.
*/
app.post('/api/neighbourhoods', async (req, res) => {
const allNeighbourhoods = await getAllData();
for (let i = 0; i < allNeighbourhoods.length; i++) {
const neighborhood = new Neighborhood(allNeighbourhoods[i])
try {
await neighborhood.save()
} catch (e) {
log(e)
res.status(400).send('Bad Request')
}
}
res.status(200).send(allNeighbourhoods)
})
// Get all neighbourhoods
app.get('/api/neighbourhoods', async(req, res) => {
try {
const neighbourhoods = await Neighborhood.find({}) // -_id
if (neighbourhoods.length !== 0) {
res.status(200).send(neighbourhoods)
} else {
res.status(500).send("Server error")
}
} catch (e) {
log(e)
res.status(500).send("Server error")
}
})
/*** REVIEWS API ROUTES ***/
app.post('/api/reviews', authenticateUser, async (req, res) => {
const review = new Review({
userId: req.user._id, // username from authenticate middleware
username: req.user.username,
neighbourhoodId: req.body.neighbId,
neighbourhoodName: req.body.neighbourhoodName,
review: {
reviewTitle: req.body.review.reviewTitle,
userRating: req.body.review.userRating,
reviewBody: req.body.review.reviewBody,
date: new Date().getFullYear()+'-'+("0"+(new Date().getMonth()+1)).slice(-2)+'-'+("0"+new Date().getDate()).slice(-2)
}
})
try {
const newReview = await review.save();
res.status(200).send(review)
} catch (e) {
log(e)
res.status(500).send("Internal server error")
}
})
// get reviews by a username removed authenticateUser for now
app.get('/api/reviews/user=:userId', async (req, res) => {
const userId = req.params.userId
try {
const usersReviews = await Review.find({userId: userId})
res.status(200).send(usersReviews)
} catch (e) {
log(e)
res.status(500).send("Internal server error")
}
})
// get reviews by a neighbourhood ID.
app.get('/api/reviews/neighbourhood=:id', async (req, res) => {
const id = req.params.id
try {
const neighborhoodReviews = await Review.find({neighbourhoodId: id}, '-_id')
if (neighborhoodReviews.length === 0) {
// log("No reviews found")
res.status(204).send("Not found")
} else {
res.status(200).send(neighborhoodReviews)
}
} catch (e) {
log(e)
res.status(500).send("Internal server error")
}
})
// ADD PATCH ROUTE LATER (TO EDIT A REVIEW)
// Delete a users review. Only the person who created the review, or if the user is an admin, can delete.
// In authenticateUser, probably add a "isAdmin" to allow for admin panel deletion.
app.delete('/api/reviews/:id', authenticateUser, async (req, res) => {
const id = req.params.id
// Validate id
if (!ObjectId.isValid(id)) {
res.status(404).send('Resource not found')
return;
}
// Delete a review by their id
try {
const review = await Review.findById(id)
// Authenticating deletion. If the user is an admin, they can delete anyones review.
if (!req.user._id.equals(review.userId) && !req.user.isAdmin) { // req.user coming from authenticate middleware
log("You cannot delete another users review")
return res.status(401).send("Unauthorized")
}
const deletion = await review.remove();
if (!review) {
res.status(404).send()
} else {
res.send(review)
}
} catch(error) {
log(error)
res.status(500).send() // server error, could not delete.
}
})
app.delete('/api/users/:id', authenticateUser, async (req, res) => {
const id = req.params.id
// Validate id
if (!ObjectId.isValid(id)) {
res.status(404).send('Resource not found')
return;
}
// Delete a review by their id
try {
const user = await User.findById(id)
// Authenticating deletion. If the user is an admin, they can delete anyones review.
if (!req.user.isAdmin) {
log("You cannot delete another users review")
return res.status(401).send("Unauthorized")
}
const deletion = await user.remove();
if (!user) {
res.status(404).send()
} else {
const users = await User.find({}, 'email username isAdmin _id')
res.send(users)
}
} catch(error) {
log(error)
res.status(500).send() // server error, could not delete.
}
})
app.use(express.static(path.join(__dirname, "/client/build")));
// All routes other than above will go to index.html
app.get("*", (req, res) => {
// check for page routes that we expect in the frontend to provide correct status code.
const goodPageRoutes = ["/", "/Login", "/Profile", '/edit', '/AboutUs', '/Neighbourhoods', '/AdminDashboard', '/Rankings'];
if (!goodPageRoutes.includes(req.url)) {
// if url not in expected page routes, set status to 404.
res.status(404).sendFile(path.join(__dirname, "404.html"))
}
// send index.html
res.sendFile(path.join(__dirname, "/client/build/index.html"));
});
/*************************************************/
// Express server listening...
const port = process.env.PORT || 5000;
app.listen(port, () => {
console.log(`Listening on port ${port}...`);
});