Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
58 lines (48 loc) · 1.35 KB
/
Copy pathapp.js
File metadata and controls
58 lines (48 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
const express = require("express");
const { projects } = require("./data.json");
const app = express();
let err = new Error();
app.set("view engine", "pug");
app.use("/static", express.static(`public`));
app.get("/", function (req, res) {
res.render("index", { projects });
});
app.get("/about", function (req, res) {
res.render("about");
});
app.get("/project/:id", function (req, res, next) {
const projectId = req.params.id;
const project = projects.find(({ id }) => id === parseInt(projectId));
if (project) {
res.render("project", { project });
} else {
err.message = `Not found, it looks like the page you're looking for does not exist`;
err.status = 404;
console.log(err);
next(err);
}
});
app.get("/error", (req, res, next) => {
err.message = `Server error`;
err.status = 500;
console.log(err);
next(err);
});
app.use((req, res, next) => {
err.message = `Not found, it looks like the page you're looking for does not exist`;
err.status = 404;
console.log(err);
next(err);
});
app.use((err, req, res, next) => {
res.locals.error = err;
res.status(err.status);
if (err.status === 404) {
res.render("page-not-found", { err });
} else if (err.status === 500) {
res.render("error", { err });
}
});
app.listen(process.env.PORT || 3000, () => {
console.log("The app is running on localhost:3000");
});