-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignment1.js
More file actions
50 lines (44 loc) · 1.51 KB
/
assignment1.js
File metadata and controls
50 lines (44 loc) · 1.51 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
const http = require('http');
const fs = require('fs');
const server = http.createServer(handleRequests);
server.listen(3000);
function handleRequests (req, res) {
const url = req.url;
const method = req.method;
if (url === '/') {
res.write('<html>');
res.write('<head><title>Home Page</title></head>');
res.write('<body><a href="/users">users</a></body>');
res.write('</html>');
return res.end();
}
if(url === '/users') {
res.write('<html>');
res.write('<head><title>Users Page</title></head>');
res.write('<body><form action="/createUser" method="POST"><input type="text" name="username"></input><button type="submit">Submit</button></form></body>');
res.write('</html>');
res.end();
}
if(url === '/createUser' && method === 'POST') {
const body = [];
req.on('data', (data) => {
body.push(data);
console.log(body);
})
req.on('end', () => {
console.log(body);
const username = Buffer.concat(body).toString().split('=')[1];
console.log(username);
fs.writeFileSync("usernames", username);
res.statusCode = 302;
res.setHeader ('Location', '/');
return res.end();
})
}
if(url === '/listUsers') {
res.write('<html>');
res.write('<head><title>List Users</title></head>');
res.write('<body><a href="/home">Home</a></body>');
res.write('</html>');
}
}