-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
113 lines (92 loc) · 3.37 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
/*jslint es6:true */
require('./utils.js');
require('dotenv').config();
// get all the route files
const profileRoutes = require('./routes/profileRoutes.js');
const searchRoutes = require('./routes/searchRoutes.js');
const authenticationRoutes = require('./routes/authenticationRoutes.js');
const favoritesRoutes = require('./routes/favoritesRoutes.js');
const customizationRoutes = require('./routes/customizationRoutes.js');
const apiRoutes = require('./routes/apiRoutes.js');
const cleanDatabaseRecipeParts = require('./routes/cleanDatabaseRecipeParts.js');
const recipeDetailsRoutes = require('./routes/recipeDetailsRoutes.js');
const express = require('express');
const sessions = require('express-session');
const MongoStore = require('connect-mongo');
const bcrypt = require('bcrypt');
const Joi = require('joi');
const app = express();
const port = process.env.PORT || 3000;
// mongodb connection
const mongodb_user = process.env.MONGODB_USER;
const mongodb_password = process.env.MONGODB_PASSWORD;
const mongodb_session_secret = process.env.MONGODB_SESSION_SECRET;
const mongodb_host = process.env.MONGODB_HOST;
const mongodb_database = process.env.MONGODB_DATABASE;
const node_session_secret = process.env.NODE_SESSION_SECRET;
const bodyParser = require('body-parser');
const expireTime = 1 * 60 * 60 * 1000; //expires after 1 hour (hours * minutes * seconds * millis)
var { database } = include('databaseConnection');
const userCollection = database.db(mongodb_database).collection('users');
const recipeCollection = database.db(mongodb_database).collection('recipes');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.urlencoded({ extended: false }));
app.use(express.static("public"));
app.use(express.static("scripts"))
var mongoStore = MongoStore.create({
mongoUrl: `mongodb+srv://${mongodb_user}:${mongodb_password}@${mongodb_host}/sessions`,
crypto: { secret: mongodb_session_secret }
})
app.use(sessions({
secret: node_session_secret,
store: mongoStore,
saveUninitialized: false,
resave: true
}
));
app.listen(port, () =>
console.log(`Listening on port ${port}`)
);
app.set('view engine', 'ejs');
// home route
app.get('/', async (req, res) => {
if (!req.session.authenticated) {
res.render('home');
}
else {
res.render('homeLoggedIn', { username: req.session.username });
}
});
// separation of routes
app.use('/', authenticationRoutes)
app.use('/', profileRoutes);
app.use('/', searchRoutes)
app.use('/', favoritesRoutes)
app.use('/', customizationRoutes)
app.use('/', cleanDatabaseRecipeParts)
app.use('/', apiRoutes)
app.use('/', recipeDetailsRoutes)
// recipe details page route
const { ObjectId } = require('mongodb');
app.get('/recipes/:id', async (req, res) => {
const id = req.params.id;
if (!ObjectId.isValid(id)) {
return res.status(400).send('Invalid recipe ID');
}
const recipe = await recipeCollection.findOne(
{ _id: new ObjectId(id) },
{ projection: { Name: 1, Description: 1, Images: 1, _id: 1, RecipeInstructions: 1, RecipeIngredientParts: 1, Image_Link: 1 } }
);
res.render('recipeDetails', { recipe: recipe });
});
// logout route
app.get('/logout', (req, res) => {
req.session.destroy();
res.redirect('/');
});
// catch all 404 page not found errors
app.get('*', (req, res) => {
res.status(404)
res.render('404')
})