-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
114 lines (103 loc) · 2.71 KB
/
Copy pathapp.js
File metadata and controls
114 lines (103 loc) · 2.71 KB
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
const express = require("express");
const exphbs = require("express-handlebars");
const mongoose = require("mongoose");
const path = require("path");
const bodyParser = require("body-parser");
const session = require("express-session");
const passport = require("passport");
const methodOverride = require("method-override");
const expressValidator = require("express-validator");
const flash = require("connect-flash");
// INITIALIZE APP
const app = express();
// MODELS
require("./models/User");
require("./models/Journal");
// CONFIGS
// mongoDB
const db_secret_key = require("./config/db_secret_key");
// passport
require("./config/passport")(passport);
// CONNECT TO DB
mongoose
.connect(db_secret_key.mongoURI)
.then(() => console.log("we are connected to our DB"))
.catch(err => console.log(err));
// HELPERS
const {
truncate,
stripper,
formatDate,
select,
editableIfUser
} = require("./helpers/hbs");
// MIDDLEWARES
// express-session
app.use(
session({
secret: db_secret_key.secretOrKey,
// secret: "SuperDuperSecret",
resave: false,
saveUninitialized: false
})
);
// passport
app.use(passport.initialize());
app.use(passport.session());
// body parser middleware
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// express validator
app.use(expressValidator());
// connect flash
app.use(flash());
// Global consts
app.use((req, res, next) => {
res.locals.success_msg = req.flash("success_msg");
res.locals.error_msg = req.flash("error_msg");
res.locals.errors = req.flash("errors");
next();
});
// method-override
app.use(methodOverride("_method"));
// express-handlebars
app.engine(
"handlebars",
exphbs({
helpers: {
// truncates the journals at the public journals
truncate,
// strips the HTML tag at the public journals
stripper,
// formats the date using moment
formatDate,
// selects the properly selected option for the edit form page
select,
// shows the edit button if you are the user on journals
editableIfUser
},
defaultLayout: "main"
})
);
app.set("view engine", "handlebars");
// GLOBAL VARIABLES
app.use((req, res, next) => {
// have access to current user data via the user variable, thanks to passport
res.locals.user = req.user || null;
next();
});
// ROUTES
const index = require("./routes/index");
const auth = require("./routes/auth");
const journals = require("./routes/journals");
// USE ROUTES
app.use("/", index);
app.use("/auth", auth);
app.use("/journals", journals);
// STATIC FOLDER
app.use(express.static(path.join(__dirname, "public")));
// SET PORT
const port = process.env.PORT || 5000;
app.listen(port, () => {
console.log(`we are live at ${port}`);
});