-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
60 lines (55 loc) · 1.53 KB
/
index.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
const http = require('http');
const PORT = 3000;
const friends = [
{
id: 0,
name: 'sir Ross',
},
{
id: 1,
name: 'sir Voss',
},
{
id: 2,
name: 'sir Boss',
}
]
const server = http.createServer((req, res) => {
const items = req.url.split('/');
if (req.method === 'POST' && items[1] === 'friends') {
req.on('data', (data) => {
const friend = data.toString();
console.log('Request:', friend);
friends.push(JSON.parse(friend));
});
req.pipe(res);
} else if (req.method === 'GET' && items[1] === 'friends') {
res.writeHead(200, {
'Content-Type': 'application/json',
});
//const friendsIndex = Number(items[2]);
if (items.length === 3) {
const friendsIndex = +items[2];
res.end(JSON.stringify(friends[[friendsIndex]]))
} else {
res.end(JSON.stringify(friends));
}
} else if (req.method === 'GET' && items[1] === 'messages') {
res.setHeader('Content-Type', 'text/html');
res.write('<html>');
res.write('<body>');
res.write('<ul>');
res.write('<li>Hello Ross!</li>');
res.write('<li>What are your thoughts on astronomy</li>');
res.write('</ul>');
res.write('</body>');
res.write('</html>');
res.end();
} else {
res.statusCode = 404;
res.end();
}
});
server.listen(PORT, () => {
console.log(`listening on port ${PORT}`);
});