-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignment.js
More file actions
40 lines (35 loc) · 1.21 KB
/
assignment.js
File metadata and controls
40 lines (35 loc) · 1.21 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
const http = require('http');
const server = http.createServer((req, res) => {
const url = req.url;
const method = req.method;
if (url === '/') {
res.setHeader('Content-Type', 'text/html');
res.write('<html>');
res.write('<head><title>Assignment 1</title></html>');
res.write('<body><form action="/create-user" method="POST"><input type="text" name="username"><button type="submit">Send</button></form></body>');
res.write('</html>');
return res.end();
}
if (url === '/users') {
res.setHeader('Content-Type', 'text/html');
res.write('<html>');
res.write('<head><title>My First Page</title><head>');
res.write('<body><ul><li>User 1</li><li>User 2</li></ul></body>');
res.write('</html>');
return res.end();
}
if(url === '/create-user'){
const body = [];
req.on('data', (chunk) => {
body.push(chunk);
});
req.on('end', () => {
const parsedBody = Buffer.concat(body).toString();
console.log(parsedBody.split('=')[1]);
});
res.statusCode = 302;
res.setHeader('Location', '/');
res.end();
}
})
server.listen(3000);