forked from AAldrich16/SDEV_255_Final_Project_Group8
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
64 lines (55 loc) · 2.08 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
require('dotenv').config();
const express = require('express');
const mongoose = require('mongoose');
// const bodyparser = require('body-parser');
const cookieParser = require('cookie-parser');
const homeRouter = require('./routes/home');
const courseRouter = require('./routes/course');
const createRouter = require('./routes/create');
const addRouter = require('./routes/add');
const loginRouter = require('./routes/login');
const registerRouter = require('./routes/register');
const editCourseRouter = require('./routes/editCourse');
const logoutRouter = require('./routes/logout');
const cartRouter = require('./routes/cart');
const User = require("./models/users");
// Express App
const app = express();
// app.use(bodyparser.json()); // bodyparser doesn't seem to do anything
app.use(cookieParser());
// Connect to database
const dbUri = `mongodb+srv://${process.env.DB_USERNAME}:${process.env.DB_PASSWORD}@sdev-255-final.aqsnzds.mongodb.net/course-registration-db?retryWrites=true&w=majority`
mongoose.set('strictQuery', true);
mongoose.connect(dbUri, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => {
console.log('Connected to database');
const port = process.env.PORT || 3000;
app.listen(3000, () => {
console.log(`App listening on port ${port}`);
});
})
.catch(err => { console.log(err) });
// Middleware
app.set('view engine', 'ejs');
app.use(express.urlencoded({ extended: true })); // This middleware is parsing the body
app.use(express.static('public'));
// Routes
app.use('/', homeRouter)
app.use('/course', courseRouter);
app.use('/create', createRouter);
app.use('/add', addRouter);
app.use('/login', loginRouter);
app.use('/register', registerRouter);
app.use('/editCourse', editCourseRouter);
app.use('/cart', cartRouter);
app.use('/logout', logoutRouter);
// 404 page
app.use(async (req, res) => {
await User.findOne({"_id": req.cookies.user}).then(function(user) {
if (user) {
res.status(404).render('404.ejs', {title: '404', usr: user});
} else {
res.render('/login');
}
});
});