forked from byui-cse/cse341-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
140 lines (119 loc) · 4.09 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
/*******************************************************************************
* Feel free to remove this comment block and all other comments after pulling.
* They're for information purposes only.
*
* This layout is provided to you for an easy and quick setup to either pull
* or use to correct yours after working at least 1 hour on Team Activity 02.
* Throughout the course, we'll be using Express.js for our view engines.
* However, feel free to use pug or handlebars ('with extension hbs'). You will
* need to make sure you install them beforehand according to the reading from
* Udemy course.
* IMPORTANT: Make sure to run "npm install" in your root before "npm start"
*******************************************************************************/
/* Packages */
const fs = require('fs');
const express = require('express');
const session = require('express-session');
const MongoDBStore = require('connect-mongodb-session')(session);
const bodyParser = require('body-parser');
const cors = require('cors');
const mongoose = require('mongoose');
const path = require('path');
const csrf = require("csurf");
const flash = require("connect-flash");
/* variables */
//main app variable
const app = express();
//cors options
const corsOptions = {
origin: "https//cse341-ssharp.herokuapp.com/",
optionsSuccessStatus: 200
}
const options = {
useUnifiedTopology: true,
useNewURLParser: true,
family: 4
};
//db setup
let mdb_url = "";
if(fs.existsSync("./cred.json")) {
let credsIn = fs.readFileSync("./cred.json")
let creds = JSON.parse(credsIn);
mdb_url = creds.url;
}
const PORT = process.env.PORT || 5000; // So we can run on heroku || (OR) localhost:5000
const MONGODB_URL = process.env.MONGODB_URL || mdb_url;
const store = new MongoDBStore({
uri: MONGODB_URL,
collection: "sessions"
});
//csrf variable
const csrfProtection = csrf();
/* Router */
const eCommerceRouter = require('./routes/eCommerceRoutes');
/* Initialize Model */
const eCommModel = require('./Model/products');
eCommModel.fetchProducts();
/* Initialize app */
app
//core setup
.use(cors(corsOptions))
.set('views', path.join(__dirname, 'views')) // set up the views folder
.set('view engine', 'ejs') // set up EJS
.use(bodyParser({ extended: false })) // For parsing the body of a POST
// express
.use(express.static(path.join(__dirname, 'public'))) // define our public folder for images/stylesheets/public js
.use(session({secret: 'secretlysecretpassthing', resave: false, saveUninitialized: false, store:store})) // sessions
// additional middleware
.use(csrfProtection) // add our csrf protection
.use(flash()) // flash for sending quick feedback
//these are variables that the res will pretty much always need
.use((req, res, next) => {
res.locals.path = req.url;
res.locals.loggedIn = req.session.loggedIn;
if(req.session.permissions != null)
res.locals.permissions = req.session.permissions;
else
res.locals.permissions = [];
res.locals.csrfToken = req.csrfToken();
next();
})
// router
.use('/', eCommerceRouter)
// default indices
.get('/', (req, res, next) => { // This is the primary index, always handled last.
res.redirect("/shop");
})
.use((req, res, next) => { // 404 page
res.locals.loggedIn = req.session.loggedIn;
if(req.session.permissions != null)
res.locals.permissions = req.session.permissions;
else
res.locals.permissions = [];
res.locals.csrfToken = req.csrfToken();
res.render('pages/404', { path: req.url });
})
.use((err, req, res, next) => { // 500 page
res.locals.loggedIn = req.session.loggedIn;
if(req.session.permissions != null)
res.locals.permissions = req.session.permissions;
else
res.locals.permissions = [];
res.locals.csrfToken = req.csrfToken();
console.log(err.stack);
res.status(500);
res.render('pages/500', {
path: "/500"
});
});
/* Connect to Database and Start Server */
mongoose
.connect(
MONGODB_URL, options
)
.then(result => {
app.listen(PORT, () => console.log(`Listening on ${PORT}`));
})
.catch(err => {
console.log(err);
});