Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
77 lines (62 loc) · 1.93 KB
/
Copy pathapp.js
File metadata and controls
77 lines (62 loc) · 1.93 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
import express from "express";
import expressEjsLayouts from "express-ejs-layouts";
import methodOverride from "method-override";
import { dirname, join } from "path";
import { fileURLToPath } from "url";
import session from "express-session";
import flash from "connect-flash";
import passport from "passport";
import MongoStore from "connect-mongo";
import { configDotenv } from "dotenv";
import connectDB from "./server/config/db.js";
import mainRoutes from "./server/routes/index.js";
import dashboardRoutes from "./server/routes/dashboard.js";
import authRoutes from "./server/routes/auth.js";
// Get the directory name of the current module
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const app = express();
const port = 5000 || process.env.PORT;
configDotenv();
app.use(
session({
secret: "indomitable password",
resave: false,
saveUninitialized: true,
store: MongoStore.create({
mongoUrl: process.env.MONGODB_URI,
}),
cookie: { maxAge: 1000 * 60 * 60 },
})
);
app.use(flash());
app.use(passport.initialize());
app.use(passport.session());
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(methodOverride("_method"));
// Connect to Database
connectDB();
// Static Files
app.use(express.static(join(__dirname, "public")));
// Templating Engine
app.use(expressEjsLayouts);
app.set("layout", "./layouts/main");
app.set("view engine", "ejs");
app.use("/", (req, res, next) => {
res.locals.currentUser = req.user;
res.locals.searchTerm = "";
res.locals.success = req.flash("success");
res.locals.error = req.flash("error");
next();
});
// Routes
app.use("/", mainRoutes);
app.use("/", dashboardRoutes);
app.use("/", authRoutes);
// Handle 404
app.get("*", function (req, res) {
// res.status(404).send("404 Page Not Found");
res.status(404).render("404");
});
app.listen(port, () => console.log(`App listening on port ${port}`));