-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
195 lines (172 loc) · 4.53 KB
/
index.js
File metadata and controls
195 lines (172 loc) · 4.53 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
const express = require("express");
const path = require("path");
const fs = require("fs");
const app = express();
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "views"));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use((req, res, next) => {
console.log("Path:", req.path);
next();
});
app.get("/", (req, res) => {
res.render("index");
});
app.get("/users", (req, res) => {
const filters = req.query;
let data = [];
if (fs.existsSync("users.json")) {
const file = fs.readFileSync("users.json").toString();
data = JSON.parse(file);
}
const filtered = data.filter((user) => {
for (const key in filters) {
const userValue = user[key];
const filterValue = filters[key];
if (typeof userValue === "string" && typeof filterValue === "string") {
if (userValue.toLowerCase() !== filterValue.toLowerCase()) {
return false;
}
} else {
if (userValue != filterValue) {
return false;
}
}
}
return true;
});
res.json(filtered);
});
app.get("/add-user", (req, res) => {
res.render("add-user");
});
app.post("/users", (req, res) => {
const user = req.body;
if (!user || !user.name || !user.email) {
return res.status(400).send("Invalid user data.");
}
if (user.name.toLowerCase() === "john") {
return res.send("John name is not accepted");
}
let data = [];
if (fs.existsSync("users.json")) {
const file = fs.readFileSync("users.json").toString();
data = JSON.parse(file);
}
const alreadyExists = data.find(
(u) => u.email.toLowerCase() === user.email.toLowerCase()
);
if (alreadyExists) {
return res.status(409).send(`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>User Added</title>
<style>
body {
font-family: Arial, sans-serif;
background: #f4f6f8;
padding: 40px;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.message-box {
background: #fff;
padding: 30px 40px;
border-radius: 8px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
text-align: center;
}
h2 {
color: #28a745;
margin-bottom: 20px;
}
a {
display: inline-block;
margin: 10px;
padding: 10px 20px;
background: #007bff;
color: #fff;
text-decoration: none;
border-radius: 5px;
transition: background 0.3s ease;
}
a:hover {
background: #0056b3;
}
</style>
</head>
<body>
<div class="message-box">
<h2>⚠️ User with this email already exists.</h2>
<a href="/">🏠 Back to Home</a>
<a href="/users">📋 View Users</a>
</div>
</body>
</html>
`);
}
data.push(user);
fs.writeFileSync("users.json", JSON.stringify(data, null, 2));
res.send(`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>User Added</title>
<style>
body {
font-family: Arial, sans-serif;
background: #f4f6f8;
padding: 40px;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.message-box {
background: #fff;
padding: 30px 40px;
border-radius: 8px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
text-align: center;
}
h2 {
color: #28a745;
margin-bottom: 20px;
}
a {
display: inline-block;
margin: 10px;
padding: 10px 20px;
background: #007bff;
color: #fff;
text-decoration: none;
border-radius: 5px;
transition: background 0.3s ease;
}
a:hover {
background: #0056b3;
}
</style>
</head>
<body>
<div class="message-box">
<h2>✅ User added successfully!</h2>
<a href="/">🏠 Back to Home</a>
<a href="/users">📋 View Users</a>
</div>
</body>
</html>
`);
});
app.use((req, res) => {
res.redirect("/");
});
app.listen(3000, () => {
console.log("App running at http://localhost:3000");
});