-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
78 lines (61 loc) · 2.06 KB
/
index.js
File metadata and controls
78 lines (61 loc) · 2.06 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
// app.js
import express from 'express';
import path from 'path';
import { fileURLToPath } from 'url';
import bodyParser from 'body-parser';
import cookieParser from 'cookie-parser';
import dotenv from 'dotenv';
import session from "express-session";
import flash from "connect-flash";
import loadModules from './src/config/load_modules.js';
import staticFiles from "./src/config/staticFiles.js";
import configureViewEngine from './src/config/viewEngine.js';
import eventLogger from './src/middlewares/logger.middleware.js';
import conditionalRendering from './src/middlewares/conditionalRender.middleware.js';
import { active_page } from './src/middlewares/activePage.js';
import { authorizeByPrefix } from './src/middlewares/rolePrefix.middleware.js';
import { attachUser } from './src/middlewares/auth.middleware.js';
dotenv.config();
// Resolve __dirname for ES modules
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
// Setup Handlebars view engine
configureViewEngine(app);
// Middleware: cookies + parsing
app.use(cookieParser());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// ⚡ Session must come BEFORE flash
app.use(session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
}));
// ⚡ Flash depends on session
app.use(flash());
// ⚡ Make flash available globally (must come after flash())
app.use((req, res, next) => {
res.locals.messages = {
success: req.flash("success"),
error: req.flash("error"),
info: req.flash("info"),
warning: req.flash("warning"),
};
next();
});
// ⚡ Attach user AFTER session/flash so it can set flash messages if needed
app.use(attachUser);
// Now the rest
app.use(eventLogger);
app.use(staticFiles);
app.use(authorizeByPrefix);
app.use(conditionalRendering);
app.use(active_page);
// Load dynamic routes
await loadModules(app);
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`🚀 Server running at http://localhost:${PORT}`);
});
export default app;