-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
73 lines (58 loc) · 1.79 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
import "dotenv/config";
import express from "express";
import path, { dirname } from "path";
import cookieParser from "cookie-parser";
import { fileURLToPath } from "url";
import session from "express-session";
import flash from "connect-flash";
import bodyParser from "body-parser";
const app = express();
const port = process.env.PORT || 3000;
// connect dB
import connectDB from "./config/db.js";
connectDB();
// Import the job scheduler
import { scheduleUserDeletionJob } from "./utils/utils.js";
scheduleUserDeletionJob();
// Get the current directory for ES modules
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// view engine setup
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "ejs");
// css and js file setup
app.use(express.static(path.join(__dirname, "public")));
// Parse URL-encoded and JSON request bodies
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(cookieParser());
// setup bodyParser middleware
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// Setup session middleware
app.use(
session({
secret: process.env.SESSION_SECRET_KEY,
resave: false,
saveUninitialized: false,
cookie: { maxAge: 60000 },
})
);
// Setup flash middleware
app.use(flash());
// import routes
import authRouter from "./routes/authRoutes.js";
import profileRouter from "./routes/profileRoutes.js";
// Use router
app.use("/", authRouter);
app.use("/profile", profileRouter);
// Handle 404 errors (not found)
app.use((req, res, next) => {
res.status(404).render("404");
});
// Global error handler
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).render("error");
});
app.listen(port, () => console.log(`App listening on port ${port}`));