-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
71 lines (59 loc) · 1.92 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
68
69
70
71
import express from "express";
import { readFile } from "fs/promises";
const app = express();
app.set("view engine", "pug");
app.set("views", "./views");
const jsonData = "./data/db.json";
const loadData = async () => {
return JSON.parse(await readFile(jsonData, "utf8"));
};
app.get(["/users", "/"], function (req, res) {
loadData().then((data) => {
const users = data.users;
res.render("index", { users, title: "List of users" });
});
});
app.get("/users/male", function (req, res) {
loadData().then(() => {
const users = data.users.filter((u) => u.gender === "male");
res.render("index", { users, title: "List of male users" });
});
});
app.get("/users/female", function (req, res) {
db.read().then(() => {
const users = data.users.filter((u) => u.gender === "female");
res.render("index", { users, title: "List of female users" });
});
});
app.get("/users/under30", function (req, res) {
loadData().then(() => {
const users = data.users.filter((u) => u.age < 30);
res.render("index", { users, title: "List of under 30 users" });
});
});
app.get("/users/over30", function (req, res) {
loadData().then(() => {
const users = data.users.filter((u) => u.age > 30);
res.render("index", { users, title: "List of over 30 users" });
});
});
app.get("/users/:id", function (req, res) {
loadData().then(() => {
const user = data.users.find((u) => u.id === parseInt(req.params.id));
res.render("show_user", {
user,
title: `Details of user ${user.firstName} ${user.lastName} (#${user.id})`,
});
});
});
app.get("/users/:id/posts", function (req, res) {
loadData().then(() => {
const user = data.users.find((u) => u.id === parseInt(req.params.id));
const posts = data.posts.filter((p) => p.userId === user.id);
res.render("posts", {
posts,
title: `Posts of user ${user.firstName} ${user.lastName} (#${user.id})`,
});
});
});
export default app;