-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
58 lines (45 loc) · 1.35 KB
/
app.js
File metadata and controls
58 lines (45 loc) · 1.35 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
// app.js
import "dotenv/config";
import express from "express";
import { fileURLToPath } from "node:url";
import path from "node:path";
import setLocals from "./middlewares/setLocals.js";
import indexRouter from "./routes/indexRouter.js";
import genresRouter from "./routes/genresRouter.js";
import booksRouter from "./routes/booksRouter.js";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
app.set("views", path.resolve(__dirname, "views"));
app.set("view engine", "ejs");
app.use(express.static(path.join(__dirname, "public")));
app.use(express.urlencoded({ extended: true }));
app.use(setLocals);
app.use("/", indexRouter);
app.use("/genres", genresRouter);
app.use("/books", booksRouter);
app.use((req, res) => {
res.status(404).render("error", {
title: "Page Not Found",
code: 404,
message: "Page not found.",
url: req.originalUrl,
});
});
app.use((err, req, res, next) => {
console.error(err);
res.status(500).render("error", {
title: "Server Error",
code: 500,
message: "Something went wrong on our side.",
details: err.message || err,
});
});
const PORT = process.env.PORT || 6969;
app.listen(PORT, (err) => {
if (err) {
console.error("❌ Server Error:", err);
return;
}
console.log(`✓ Server running on http://localhost:${PORT}`);
});