-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchore1.js
88 lines (84 loc) · 3.41 KB
/
chore1.js
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
const http = require('http');
const fs = require('fs');
const fullHtml = require('./FullHtml');
const component = require('./component');
const updateUsers= (userName)=>{
return new Promise((res)=>{
fs.readFile('users.txt', (error, data)=>{
if (error){
res("");
} else {
res(data.toString());
}
})
}).then((fulfill)=>{
let users = fulfill!==""?fulfill.split(",").map((user)=>user.trim()):[];
if (userName.trim()&&!users.includes(userName.trim())){
users.push(userName.trim());
}
return new Promise((res, rej)=>{
fs.writeFile("users.txt", users.sort(), (error)=>{
if (error){
rej(error);
} else {
res(userName + " successfully written to users file");
}
})
})
}, (unfulfill)=>{
});
}
const s = http.createServer((req, res)=>{
const url = req.url;
switch (url){
case "/users":
new Promise((res, rej)=>{
fs.readFile("users.txt", (error, data)=>{
if (error){
rej(error);
}else{
res(data.toString().split(",").map((da)=>da.trim()));
}
})
}).then((fulfill)=>{
const lines = [];
fulfill.forEach(element => {
let line = component({content:element, type:"li"});
lines.push(line);
});
const list = component({content: lines, type: "ul"});
const title = component({content: "Hall of fame",type: "h1"});
const returnToTitle = component({content: "Return to main page", type:"button", attributes:'type="submit"'});
const sendform = component({content: returnToTitle, type: "form", attributes: 'action="/" method="GET"'});
res.write(fullHtml({body:[title, list, sendform]}));
res.end();
}, (rejected)=>{
console.log(rejected);
})
break;
case "/create-user":
const body = [];
req.on('data', (chunk)=>{
body.push(chunk);
});
req.on('end', ()=>{
const parsedBody = Buffer.concat(body).toString();
const message = parsedBody.split("=")[1];
updateUsers(message).then(()=>{
res.statusCode=302;
res.setHeader("Location", "/users");
res.end();
});
});
break;
default:
const input = component({type:"input" ,selfClosing: true, attributes:['name="username"', 'type="text"']});
const submit = component({content:"Submit", type: "button", attributes:'type="submit"'});
const login = component({content:[input, submit], type:"form", attributes:['method="POST"', 'action="/create-user"']});
const title = component({content:"Hello there!",type:"h1"});
res.write(fullHtml({body:[title, login]}));
res.end();
break;
}
});
s.listen(8080);