-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
74 lines (65 loc) · 1.75 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { createServer } from 'node:http';
import fs from 'node:fs'
import { changeStatus, createTask, getData, initCounter, deleteTask } from './storage.js';
// const pages = './pages'
const hostname = '127.0.0.1';
const port = 3002;
function init() {
initCounter()
}
const server = createServer((req, res) => {
const url = req.url
const method = req.method
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
if (url === '/tasks' && method === 'POST') {
let body = [];
req.on('data', chunk => {
body.push(chunk);
console.log(chunk);
}).on('end', () => {
body = Buffer.concat(body).toString();
const item = JSON.parse(body)
const result = createTask(item)
console.log(result);
res.end(result)
});
}
// TODO Change Status
if (url === '/tasks' && method === 'PATCH') {
let body = [];
req.on('data', chunk => {
body.push(chunk);
console.log(chunk);
}).on('end', () => {
body = Buffer.concat(body).toString();
const requestData = JSON.parse(body)
const changedData = changeStatus(requestData.id)
res.end(changedData)
});
}
// ToDO Delete Task
if (url === '/tasks' && method === 'DELETE') {
let body = [];
req.on('data', chunk => {
body.push(chunk);
}).on('end', () => {
body = Buffer.concat(body).toString();
const requestData = JSON.parse(body);
const result = deleteTask(requestData.id);
res.end();
});
}
if (url === '/tasks' && method === 'GET') {
const result = getData()
res.end(result)
}
});
server.listen(port, hostname, () => {
init()
console.log(`Server running at http://${hostname}:${port}/`);
});
// {
// "text": "Hello",
// "isDone": "true"
// }