-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
66 lines (52 loc) · 2.14 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
require('dotenv').config()
const express = require("express"),
app = express(),
mongoose = require("mongoose"),
passport = require("passport"),
flash = require("connect-flash"),
{ printLog } = require("./utilities/helpers"),
{ addUserToLocals, addFlashMessagesToLocals } = require("./utilities/middleware");
// ===== DATABASE =============================================================
// Connect to the database.
mongoose.connect(process.env.MONGODB_URL, {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: true
}).then(() => {
printLog("Connection to database has been established.");
}).catch(err => {
printLog("ERROR: Failed to connect to the database.");
printLog(err);
// TODO: show error to client
});
mongoose.set('useCreateIndex', true);
// ===== APP SERVER SETUP =====================================================
app.set("view engine", "ejs");
app.use(express.urlencoded({ extended: true }));
app.use(express.static(__dirname + "/public"));
app.use(require("method-override")("_method"));
// ===== PASSPORT SETUP =======================================================
const User = require("./models/user");
passport.use(User.createStrategy());
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
app.use(require("express-session")({
secret: "+#3_qU!cK_bR0wN_f0X",
resave: false,
saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(addUserToLocals);
// ===== FLASH SETUP ==========================================================
app.use(flash()); //must be declared after the session
app.use(addFlashMessagesToLocals);
// ===== ROUTES SETUP =========================================================
app.use(require("./controllers/auth"));
app.use(require("./controllers/parks"));
app.use(require("./controllers/comments"));
app.use(require("./controllers/reviews"));
// ===== LISTEN ================================================================
app.listen(process.env.PORT, () => {
printLog("The Park Discovery server is started.");
});