Skip to content

Commit aad7602

Browse files
work on lab 5
1 parent dd2e96b commit aad7602

File tree

13 files changed

+141
-90
lines changed

13 files changed

+141
-90
lines changed

labs/lab5/dynamic-site/index.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import http from "node:http";
2+
import fs from "node:fs";
3+
import path from "node:path";
4+
5+
const port = 8000;
6+
const host = "localhost";
7+
8+
const requestListener = (req, res) => {
9+
console.log("Method: ", req.method);
10+
if (req.method == "GET") {
11+
getHandler(req, res);
12+
} else if(req.method == "POST") {
13+
if (req.url = "/login") {
14+
loginHandler(req, res);
15+
} else if (req.url = "/register") {
16+
registerHandler(req, res);
17+
}
18+
}
19+
}
20+
21+
const server = http.createServer(requestListener);
22+
server.listen(port, host, () => {
23+
console.log("Server running...");
24+
})
25+
26+
function getHandler(req, res) {
27+
const url = req.url;
28+
let filePath = './public' + (url === "/" ? '/index.html' : url);
29+
let ext = path.extname(filePath);
30+
if(ext == "") {
31+
filePath += ".html";
32+
ext = ".html";
33+
}
34+
35+
36+
fs.readFile(filePath, (err, data) => {
37+
if (err) {
38+
console.error(err);
39+
res.writeHead(404, { 'Content-Type': 'text/html' });
40+
res.end("<h1>404 Not Found</h1>");
41+
return;
42+
}
43+
44+
45+
const mimeTypes = {
46+
'.html': 'text/html',
47+
'.css': 'text/css',
48+
'.js': 'text/javascript',
49+
};
50+
51+
const contentType = mimeTypes[ext] || 'text/html';
52+
53+
res.writeHead(200, {'Content-Type': contentType});
54+
res.write(data);
55+
res.end();
56+
})
57+
}
58+
59+
function loginHandler(req, res) {
60+
res.writeHead(500, {'Content-Type': 'application/json'})
61+
const response = {
62+
message: "error"
63+
};
64+
res.end(JSON.stringify(response));
65+
}
66+
67+
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));
73+
}

labs/lab5/dynamic-site/js/login.js

Lines changed: 0 additions & 16 deletions
This file was deleted.

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

Lines changed: 0 additions & 50 deletions
This file was deleted.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "dynamic-site",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"type": "module",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "",
10+
"license": "MIT",
11+
"description": ""
12+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<!DOCTYPE html>
22
<html lang="de">
3+
34
<head>
45
<meta charset="UTF-8">
56
<meta name="viewport" content="width=device-width, initial-scale=1.0">
@@ -11,7 +12,7 @@
1112

1213
<div class="login-container">
1314
<h2>Login</h2>
14-
<form id="loginForm">
15+
<form id="loginForm" action="/login" method="post">
1516
<input type="text" id="username" placeholder="Benutzername" required>
1617
<input type="password" id="password" placeholder="Passwort" required>
1718
<button type="submit">Einloggen</button>
@@ -21,10 +22,9 @@ <h2>Login</h2>
2122

2223
<br>
2324

24-
<a href="registration.html">Registrieren</a>
25+
<a href="registration">Registrieren</a>
2526

2627
<script src="js/login.js"></script>
27-
2828
</body>
2929

30-
</html>
30+
</html>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
document.getElementById('loginForm').addEventListener('submit', async (event) => {
2+
event.preventDefault(); // Verhindert das Standardverhalten des Formulars
3+
4+
const form = event.target;
5+
const formData = new FormData(form);
6+
7+
const response = await fetch(form.action, {
8+
method: form.method,
9+
body: JSON.stringify(formData),
10+
});
11+
12+
const body = await response.json();
13+
14+
if(!response.ok) {
15+
const message = document.getElementById('message');
16+
message.style.color = 'red';
17+
message.textContent = body.message;
18+
}
19+
20+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
document.getElementById('registerForm').addEventListener('submit', async (event) => {
2+
event.preventDefault(); // Verhindert das Standardverhalten des Formulars
3+
4+
const form = event.target;
5+
const formData = new FormData(form);
6+
7+
const response = await fetch(form.action, {
8+
method: form.method,
9+
body: JSON.stringify(formData),
10+
});
11+
12+
const body = await response.json();
13+
14+
if(!response.ok) {
15+
const message = document.getElementById('message');
16+
message.style.color = 'red';
17+
message.textContent = body.message;
18+
}
19+
20+
});

0 commit comments

Comments
 (0)