Skip to content

Commit 6b281b2

Browse files
write to file when register
1 parent aad7602 commit 6b281b2

File tree

5 files changed

+130
-28
lines changed

5 files changed

+130
-28
lines changed

labs/lab5/dynamic-site/index.js

Lines changed: 64 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ const host = "localhost";
77

88
const requestListener = (req, res) => {
99
console.log("Method: ", req.method);
10+
console.log("Url: ", req.url);
1011
if (req.method == "GET") {
1112
getHandler(req, res);
12-
} else if(req.method == "POST") {
13-
if (req.url = "/login") {
13+
} else if (req.method == "POST") {
14+
if (req.url === "/login") {
1415
loginHandler(req, res);
15-
} else if (req.url = "/register") {
16+
} else if (req.url === "/register") {
1617
registerHandler(req, res);
1718
}
1819
}
@@ -27,14 +28,14 @@ function getHandler(req, res) {
2728
const url = req.url;
2829
let filePath = './public' + (url === "/" ? '/index.html' : url);
2930
let ext = path.extname(filePath);
30-
if(ext == "") {
31+
if (ext == "") {
3132
filePath += ".html";
3233
ext = ".html";
3334
}
3435

3536

3637
fs.readFile(filePath, (err, data) => {
37-
if (err) {
38+
if (err) {
3839
console.error(err);
3940
res.writeHead(404, { 'Content-Type': 'text/html' });
4041
res.end("<h1>404 Not Found</h1>");
@@ -50,24 +51,74 @@ function getHandler(req, res) {
5051

5152
const contentType = mimeTypes[ext] || 'text/html';
5253

53-
res.writeHead(200, {'Content-Type': contentType});
54+
res.writeHead(200, { 'Content-Type': contentType });
5455
res.write(data);
5556
res.end();
5657
})
5758
}
5859

5960
function loginHandler(req, res) {
60-
res.writeHead(500, {'Content-Type': 'application/json'})
61+
res.writeHead(500, { 'Content-Type': 'application/json' })
6162
const response = {
62-
message: "error"
63+
message: "login error"
6364
};
6465
res.end(JSON.stringify(response));
6566
}
6667

6768
function registerHandler(req, res) {
68-
res.writeHead(500, {'Content-Type': 'application/json'})
69-
const response = {
70-
message: "error"
71-
};
72-
res.end(JSON.stringify(response));
69+
let body = '';
70+
71+
req.on('data', chunk => {
72+
body += chunk.toString();
73+
});
74+
75+
req.on('end', () => {
76+
let data;
77+
try {
78+
data = JSON.parse(body);
79+
} catch (error) {
80+
res.writeHead(400, { 'Content-Type': 'application/json' });
81+
res.end(JSON.stringify({ message: "Invalid Request" }));
82+
}
83+
84+
const requiredFields = ["first_name", "last_name", "username", "password", "gender", "topic"];
85+
const missingFields = requiredFields.filter(field => !data[field]);
86+
87+
if (missingFields.length > 0) {
88+
res.writeHead(400, { 'Content-Type': 'application/json' });
89+
return res.end(JSON.stringify({ message: `Missing fields: ${missingFields.join(', ')}` }));
90+
}
91+
92+
const filePath = path.join("./", 'users.json');
93+
94+
fs.readFile(filePath, 'utf8', (err, fileData) => {
95+
let users = [];
96+
97+
if (!err && fileData) {
98+
try {
99+
users = JSON.parse(fileData);
100+
} catch (jsonErr) {
101+
users = [];
102+
}
103+
}
104+
105+
const existingUser = users.find(user => user.username === data.username);
106+
if (existingUser) {
107+
res.writeHead(409, { 'Content-Type': 'application/json' });
108+
return res.end(JSON.stringify({ message: "Username already exists" }));
109+
}
110+
users.push(data);
111+
112+
fs.writeFile(filePath, JSON.stringify(users, null, 2), err => {
113+
if (err) {
114+
res.writeHead(500, { 'Content-Type': 'application/json' });
115+
return res.end(JSON.stringify({ message: "Internal Server Error" }));
116+
}
117+
118+
res.writeHead(201, { 'Content-Type': 'application/json' });
119+
res.end(JSON.stringify({ message: "User registered successfully" }));
120+
});
121+
});
122+
123+
});
73124
}

labs/lab5/dynamic-site/public/css/logins.css

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,26 @@ body {
77
align-items: center;
88
height: 100vh;
99
}
10+
1011
.login-container {
1112
background: white;
1213
padding: 20px;
1314
border-radius: 5px;
1415
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
1516
}
17+
1618
.login-container h2 {
1719
margin-bottom: 20px;
1820
}
21+
1922
.login-container input {
2023
width: 100%;
2124
padding: 10px;
2225
margin: 10px 0;
2326
border: 1px solid #ccc;
2427
border-radius: 5px;
2528
}
29+
2630
.login-container button {
2731
width: 100%;
2832
padding: 10px;
@@ -32,6 +36,23 @@ body {
3236
border-radius: 5px;
3337
cursor: pointer;
3438
}
39+
3540
.login-container button:hover {
3641
background-color: #218838;
37-
}
42+
}
43+
44+
45+
.radio-input {
46+
display: flex;
47+
justify-content: start;
48+
align-items: center;
49+
50+
input {
51+
width: auto;
52+
margin: 0 1em;
53+
}
54+
}
55+
56+
.radio-group {
57+
margin-bottom: 1em;
58+
}

labs/lab5/dynamic-site/public/js/register.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,21 @@ document.getElementById('registerForm').addEventListener('submit', async (event)
22
event.preventDefault(); // Verhindert das Standardverhalten des Formulars
33

44
const form = event.target;
5-
const formData = new FormData(form);
5+
const formData = new FormData(form);
6+
7+
const jsonData = Object.fromEntries(formData.entries());
68

79
const response = await fetch(form.action, {
810
method: form.method,
9-
body: JSON.stringify(formData),
10-
});
11+
body: JSON.stringify(jsonData),
12+
headers: {
13+
"Content-Type": "application/json"
14+
}
15+
});
1116

1217
const body = await response.json();
1318

14-
if(!response.ok) {
19+
if (!response.ok) {
1520
const message = document.getElementById('message');
1621
message.style.color = 'red';
1722
message.textContent = body.message;

labs/lab5/dynamic-site/public/registration.html

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,29 @@
1313
<div class="login-container">
1414
<h2>Login</h2>
1515
<form id="registerForm" method="POST" action="/register">
16-
<input type="text" id="first-name" placeholder="Vorname" maxlength="20" required>
17-
<input type="text" id="last-name" placeholder="Nachname" maxlength="20" required>
18-
<input type="text" id="username" placeholder="Benutzername" maxlength="20" required>
19-
<input type="password" id="password" placeholder="Passwort" maxlength="20" minlength="8" required>
20-
<label for="male">Männlich</label>
21-
<input type="radio" id="male" name="gender" value="male" checked>
16+
<input type="text" name="first_name" id="first-name" placeholder="Vorname" maxlength="20" required>
17+
<input type="text" name="last_name" id="last-name" placeholder="Nachname" maxlength="20" required>
18+
<input type="text" name="username" id="username" placeholder="Benutzername" maxlength="20" required>
19+
<input type="password" name="password" id="password" placeholder="Passwort" maxlength="20" minlength="8" required>
20+
21+
<h3>Geschlecht</h3>
22+
<div class="radio-group">
23+
<div class ="radio-input">
24+
<input type="radio" id="male" name="gender" value="male">
25+
<label for="male">Männlich</label>
26+
</div>
27+
28+
<div class ="radio-input">
29+
<input type="radio" id="female" name="gender" value="female">
30+
<label for="female">Weiblich</label>
31+
</div>
32+
33+
<div class ="radio-input">
34+
<input type="radio" id="divers" name="gender" value="divers">
35+
<label for="divers">Divers</label>
36+
</div>
37+
</div>
2238

23-
<label for="female">Weiblich</label>
24-
<input type="radio" id="female" name="gender" value="female">
2539

2640
<label for="topic">Wähle eine Interessenskategorie</label>
2741
<select name="topic" id="topic">
@@ -31,9 +45,9 @@ <h2>Login</h2>
3145
<option value="flexbox">FlexBox</option>
3246
<option value="gridbox">GridBox</option>
3347
</select>
34-
<input type="text" id="comment" placeholder="Kommentar" maxlength="100">
48+
<input name="comment" type="text" id="comment" placeholder="Kommentar" maxlength="100">
3549
<label for="conditions"> Ich akzeptiere die Nutzungsbedingungen</label>
36-
<input type="checkbox" id="conditions" name="conditions_accepted" value="yes">
50+
<input type="checkbox" id="conditions" value="yes" required>
3751

3852
<button type="submit">Registrieren</button>
3953
</form>

labs/lab5/dynamic-site/users.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[
2+
{
3+
"first_name": "test123",
4+
"last_name": "123test",
5+
"username": "123test",
6+
"password": "1fgddrfg",
7+
"gender": "male",
8+
"topic": "html",
9+
"comment": "test123"
10+
}
11+
]

0 commit comments

Comments
 (0)