-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
67 lines (57 loc) · 1.63 KB
/
index.js
File metadata and controls
67 lines (57 loc) · 1.63 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
59
60
61
62
63
64
65
66
67
const express = require("express");
const fs = require("fs");
const path = require("path");
const app = express();
const port = 3000;
const expressLayouts = require("express-ejs-layouts");
app.use(express.static("public"));
app.use(expressLayouts);
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "views"));
app.set("layout", "layouts/layout");
//home for sign in sign out
app.get("/", (req, res) => {
res.render("home", {
title: "Home",
});
});
//search for login
app.get("/users", (req, res) => {
const { param } = req.query; // Use req.query to get the query parameter
const data = JSON.parse(fs.readFileSync("data.json", "utf8"));
if (!param) {
return res.render("users", {
filteredUsers: data.users,
param: "",
title: "All Users",
});
}
const filteredUsers = data.users.filter(
(u) =>
u.name.toLowerCase().includes(param.toLowerCase()) ||
u.surname.toLowerCase().includes(param.toLowerCase())
);
if (filteredUsers.length === 0) {
return res.render("notfound", { param });
}
res.render("users", { filteredUsers, param, title: "Search Results" });
});
//detail for user
app.get("/users/:id", (req, res) => {
const data = JSON.parse(fs.readFileSync("data.json", "utf8"));
const user = data.users.find((u) => u.id === parseInt(req.params.id));
if (!user) {
return res.status(404).send("No user");
}
res.render("user", {
user,
title: "User page",
});
});
app.post("/users", (req, res) => {
const { name, surname } = req.body;
res.send(`${name}`);
});
app.listen(port, () => {
console.log(`App listening at http://localhost:${port}`);
});