-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver2.js
More file actions
77 lines (61 loc) · 2.17 KB
/
Copy pathserver2.js
File metadata and controls
77 lines (61 loc) · 2.17 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
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
import { create } from 'domain';
import { createServer } from 'http';
const PORT = 8000;
const users = [
{ id: 1, name: 'Shiva' },
{ id: 2, name: 'Sai' }
]
const regex = /^\/api\/users\/[0-9]+$/;
//middlewares
const attachJsonHeader = (req, res, next) => {
res.setHeader('Content-Type', 'application/json');
next();
}
const createUserHandler = (req, res) => {
let body = '';
//wait for data stream to collect completely
req.on('data', chunk => {
body += chunk.toString();
});
req.on('end', () => {
//parse the collected data and create user object
const user = JSON.parse(body);
if (users.find((u) => u.id === user.id)) {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ message: 'User with this id already exists' }));
} else {
users.push(user);
res.writeHead(201, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(user));
}
});
}
const server = createServer((req, res) => {
attachJsonHeader(req, res, () => {
if (req.method === 'GET') {
if (req.url === '/users') {
res.end(JSON.stringify(users));
} else if (req.url.match(regex)) {
const id = req.url.split('/')[3];
const user = users.find((user) => (user.id === parseInt(id)))
if (user) {
res.writeHead(200, { 'Content-Type': 'application/json' })
res.end(JSON.stringify(user));
} else {
res.writeHead(404, { 'Content-Type': 'application/json' })
res.end(JSON.stringify({ message: 'User not found' }))
}
} else {
res.writeHead(404, { 'Content-Type': 'application/json' })
res.end(JSON.stringify({ message: 'Route not found' }))
}
} else if (req.method === 'POST') {
if (req.url === '/api/users') {
createUserHandler(req, res);
}
}
})
})
server.listen(PORT, () => {
console.log(`Server is running from on port ${PORT}`);
})