-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
68 lines (57 loc) · 1.52 KB
/
app.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
const express = require("express");
const dotenv = require("dotenv");
const morgan = require("morgan");
const exphbs = require("express-handlebars");
const connectDB = require("./config/db");
const flash = require("connect-flash");
const session = require("express-session");
const passport = require("passport");
dotenv.config({ path: "./config/config.env" });
require("./config/passport")(passport);
const app = express();
// logging
if (process.env.NODE_ENV === "development") {
app.use(morgan("dev"));
}
// MongoDB connection
connectDB();
// bodyparser middleware
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
// handlebars
app.engine(
".hbs",
exphbs.engine({
defaultLayout: "main",
extname: ".hbs",
})
);
app.set("view engine", ".hbs");
// session middleware
app.use(
session({
secret: "keyboard cat",
resave: false,
saveUninitialized: false,
// store: MongoStore.create({ mongoUrl: process.env.MONGO_URI }),
})
);
// passport middleware
app.use(passport.initialize());
app.use(passport.session());
// connect flash
app.use(flash());
// global varables
app.use((req, res, next) => {
res.locals.success_msg = req.flash("success_msg");
res.locals.error_msg = req.flash("error_msg");
res.locals.error = req.flash("error");
next();
});
// routes
app.use("/", require("./routes/index"));
app.use("/users", require("./routes/users"));
const PORT = process.env.PORT || 3000;
app.listen(PORT, () =>
console.log(`server is running in ${process.env.NODE_ENV} node on ${PORT}`)
);