-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
67 lines (56 loc) · 1.69 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
import dotenv from "dotenv";
dotenv.config();
import path from "path";
import { fileURLToPath } from "url";
import express from "express";
import session from "express-session";
import flash from "connect-flash";
import cors from "cors";
import expressLayouts from "express-ejs-layouts";
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: true,
cookie: { secure: false },
}));
// Get the directory of the current file (app.js)
const __dirname = path.dirname(fileURLToPath(import.meta.url));
app.use(express.static(path.join(__dirname, 'public')));
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "ejs");
app.use(expressLayouts);
app.set("view engine", "ejs");
app.use(flash());
app.use(cors({
origin: "http://localhost:3000",
}));
app.use((req, res, next) => {
res.locals.successMessage = req.flash("success");
res.locals.errorMessage = req.flash("error");
next();
});
import indexRoutes from "./routes/indexRoutes.js";
app.use("/", indexRoutes);
app.use((req, res, next) => {
const locals = { title: "404 | Page Not Found!" };
return res.status(404).render("404", {
locals,
layout: "layouts/errorLayout",
});
});
app.use((err, req, res, next) => {
console.error(err.stack);
const locals = { title: "500 | Internal Server Error!" };
return res.status(500).render("internalError", {
locals,
layout: "layouts/errorLayout",
});
});
const PORT = process.env.PORT || 4000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
export default app;