-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
118 lines (98 loc) · 3.31 KB
/
Copy pathserver.js
File metadata and controls
118 lines (98 loc) · 3.31 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
const url = require('url')
const http = require('http')
const fs = require('fs')
const PORT = 8080
http.createServer((req, res) => {
console.log(req.method)
if (req.method == "GET") {
if (req.url === "/") {
fs.readFile('contact-ms.html', (err, data) => {
if (err) {
throw err
}
res.writeHead(200, { "Content-Type": "text/html" })
res.end(data)
})
}
if (req.url === "/contact-ms.js") {
fs.readFile('contact-ms.js', (err, data) => {
if (err) {
throw err
}
res.writeHead(200, { "Content-Type": "text/javascript" })
res.end(data)
})
}
if (req.url.startsWith("/search")) {
const q = url.parse(req.url, true);
console.log(q.query);
readContact(q.query.name, number => {
if (number) {
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify({ number }));
} else {
res.writeHead(404, { "Content-Type": "application/json" });
res.end(JSON.stringify({ error: "Contact not found" }));
}
});
}
}
if (req.method == "POST") {
let body = "";
req.on("data", chunk => {
body += chunk.toString();
});
req.on("end", () => {
try {
const parsed = JSON.parse(body);
console.log("Parsed body:", parsed);
addContact(parsed.name, parsed.number)
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify({ message: "Data received", data: parsed }));
} catch (e) {
console.error("Invalid JSON", e);
res.writeHead(400, { "Content-Type": "application/json" });
res.end(JSON.stringify({ error: "Invalid JSON" }));
}
});
}
if (req.method == "DELETE" && req.url.startsWith('/name')) {
let q = url.parse(req.url, true)
deleteContact(q.query.name)
res.writeHead(200, { "Content-Type": "text/plain" })
res.end("Successfully deleted")
}
}).listen(PORT)
function addContact(name, number) {
fs.appendFile('contacts.txt', '\n' + name + ',' + number, (err) => {
if (err) throw err
})
}
function readContact(name, callback) {
fs.readFile('contacts.txt', 'utf-8', (err, data) => {
if (err) throw err;
const lines = data.split('\n');
for (let line of lines) {
const [n, num] = line.split(',');
if (n === name) {
callback(num);
return;
}
}
callback(null);
});
}
function deleteContact(_name) {
fs.readFile('contacts.txt', 'utf-8', (err, data) => {
if (err) throw err
let newLines = []
let lines = data.split('\n')
for (let line of lines) {
let [name, number] = line.split(',')
if (name !== _name) {
newLines.push(line)
}
}
fs.writeFile('contacts.txt', newLines.join('\n'), (err) => { if (err) throw err })
})
}