-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
84 lines (70 loc) · 2.21 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
const flash = require("express-flash");
const express = require("express");
const app = express();
const mongoose = require("mongoose");
const passport = require("passport");
const session = require("express-session");
const MongoStore = require("connect-mongo")(session);
const methodOverride = require("method-override");
const dotenv = require("dotenv");
const morgan = require("morgan");
const connectDB = require("./config/database");
const homeRoutes = require("./routes/home");
const recipeRoutes = require("./routes/recipe");
const authRoutes = require("./routes/auth");
const formatMessages = require("./middleware/customFunctions").formatMessages;
// Load env variables into app
dotenv.config({ path: "./config/.env" });
// Passport config
require("./config/passport")(passport);
// express middleware
app.set("view engine", "ejs");
app.use(express.static("public"));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
// Logging
if (process.env.NODE_ENV === "development") {
app.use(morgan("dev"));
}
// use forms for put & delete
app.use(methodOverride("_method"));
// Setup Sessions - stored in MongoDB
app.use(
session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
store: new MongoStore({ mongooseConnection: mongoose.connection }),
// cannot enable cookie.secure, until this app has an ssl certificate
cookie: { maxAge: 3600000 },
})
);
// Passport middleware
app.use(passport.initialize());
app.use(passport.session());
// use express-flash to display errors and success messages
app.use(flash());
app.use((req, res, next) => {
const successMsg = formatMessages(req.flash("success"));
const errorMsg = formatMessages(req.flash("errors"));
console.log("succes: ", successMsg);
console.log("error: ", errorMsg);
res.locals.success = successMsg;
res.locals.errors = errorMsg;
next();
});
// routes
app.use("/dashboard", homeRoutes);
app.use("/recipes", recipeRoutes);
app.use("/", authRoutes);
const PORT = process.env.PORT || 3001;
// connect to database
connectDB().then(() => {
app.listen(PORT, (err) => {
if (err) {
console.error(err);
} else {
console.log(`Server running on port ${PORT}`);
}
});
});