-
Notifications
You must be signed in to change notification settings - Fork 1
/
demo.js
84 lines (71 loc) · 2.18 KB
/
demo.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
const HTTP = require('http');
const todos = [
{id: 1, value: "One"},
{id: 2, value: "Two"},
{id: 3, value: "Three"},
{id: 4, value: "Four"},
]
const SERVER = HTTP.createServer((req, res) => {
// console.log(req.method);
// const {headers, url, method} = req;
const {method, url} = req;
// console.log(headers, url, method);
// res.statusCode = 404;
// res.setHeader('Content-Type', 'text/html');
// res.setHeader('Content-Type', 'application/json');
// res.setHeader('X-Powered-By', 'Node.js');
// res.writeHead(200, {
// 'Content-Type': 'application/json',
// 'X-Powered-By': 'Node.js'
// })
// res.write("<h1>Hello Sourav</h1>")
// res.write("<h2>Hello Sourav Again</h2>")
// console.log("Authorization Code : " + req.headers.authorization);
let body = [];
req.on('data', chunk => {
body.push(chunk)
}).on('end', () => {
body = Buffer.concat(body).toString();
let status = 404;
const response = {
success: false,
data: null,
error: null
}
if (method === 'GET' && url === '/todos') {
status = 200;
response.success = true;
response.data = todos
}
if (method === 'POST' && url === '/todos') {
const {id, text} = JSON.parse(body);
if (!id || !text) {
status = 400;
response.error = "Please enter ID & TEXT";
} else {
todos.push({id, text});
status = 201;
response.success = true;
response.data = todos;
}
}
res.writeHead(status, {
'Content-Type': 'application/json',
'X-Powered-By': 'Node.js'
});
res.end(JSON.stringify(response));
// console.log(body);
})
// res.end(JSON.stringify({
// success: true,
// // success: false,
// // error: "Not Found",
// // error: "Bad Request",
// // data: todos,
// data: todos
// }));
})
const PORT = 2020;
SERVER.listen(PORT, () => {
console.log(`Server Running on ${PORT}`)
});