Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
100 lines (75 loc) · 2.08 KB
/
Copy pathserver.js
File metadata and controls
100 lines (75 loc) · 2.08 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
const express = require("express");
const path = require("path");
const app = express();
const PORT = 3000;
// Form data read karne ke liye
app.use(express.urlencoded({ extended: true }));
// JSON data read karne ke liye
app.use(express.json());
// frontend folder ki files serve karna
app.use(express.static(path.join(__dirname, "frontend")));
// Temporary users storage
const users = [];
// =========================
// REGISTRATION
// =========================
// Register page open karne ke liye
app.get("/register", (req, res) => {
res.sendFile(path.join(__dirname, "frontend", "registration.html"));
});
app.post("/register", (req, res) => {
console.log(req.body);
const {
name,
password,
email,
phone,
gender,
day,
month,
year,
languages,
address
} = req.body;
if (!name || !password || !email || !phone) {
return res.redirect("/registration.html?error=missing");
}
const existingUser = users.find(user => user.email === email);
if (existingUser) {
return res.redirect("/registration.html?error=exists");
}
const newUser = {
name,
password,
email,
phone,
gender,
dob: `${day}-${month}-${year}`,
languages,
address
};
users.push(newUser);
console.log("New user registered:");
console.log(newUser);
return res.redirect("/login.html?registered=true");
});
// =========================
// LOGIN
// =========================
app.post("/login", (req, res) => {
const { email, password } = req.body;
const user = users.find(
user => user.email === email && user.password === password
);
if (!user) {
return res.redirect("/login.html?error=invalid");
}
// Success — redirect to catalogue
return res.redirect("/catalogue.html?welcome=" + encodeURIComponent(user.name));
});
// =========================
// START SERVER
// =========================
app.listen(PORT, () => {
console.log(`Server is running at http://localhost:${PORT}`);
});