-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
75 lines (60 loc) · 1.68 KB
/
Copy pathserver.js
File metadata and controls
75 lines (60 loc) · 1.68 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
import express from "express";
import path from "path";
import { fileURLToPath } from "url";
import hbs from "hbs";
import dotenv from "dotenv";
import session from "express-session";
import flash from "connect-flash";
import { noCache } from "./middlewares/cacheMiddleware.js";
import authRoutes from "./routes/authRoutes.js";
import expenseRoutes from "./routes/expenseRoutes.js";
import connectDB from "./config/db.js";
dotenv.config();
connectDB();
const app = express();
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// middleware
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
// view engine
app.set("view engine", "hbs");
app.set("views", path.join(__dirname, "views"));
hbs.registerPartials(path.join(__dirname, "views/partials"));
// handlebars helpers
hbs.registerHelper("eq", (a, b) => a === b);
// static files
app.use(express.static(path.join(__dirname, "public")));
// session (must be before routes)
app.use(
session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
cookie: {
httpOnly: true,
maxAge: 1000 * 60 * 60,
},
})
);
// cache middleware
app.use(noCache);
// flash messages (must be after session)
app.use(flash());
// make flash messages available in all views
app.use((req, res, next) => {
res.locals.success = req.flash("success");
res.locals.error = req.flash("error");
next();
});
// routes
app.use("/", authRoutes);
app.use("/expense", expenseRoutes);
// home route
app.get("/", (req, res) => {
res.render("login");
});
// server
app.listen(process.env.PORT, () => {
console.log(`Server running on port ${process.env.PORT}`);
});