-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
163 lines (149 loc) · 6.59 KB
/
server.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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
const http = require('http');
const fs = require('fs')
const path = require('path');
const url = require('url');
const formidable = require('formidable')
const replaceTemplete = (temp, res) => {
let tempData = temp.replace(/{%id%}/g, res.id);
tempData = tempData.replace(/{%title%}/g, res.title);
tempData = tempData.replace(/{%description%}/g, res.description);
return tempData;
}
const indexPage = fs.readFileSync(path.join(__dirname, 'view', 'index.html'), 'utf-8');
const updatePage = fs.readFileSync(path.join(__dirname, 'view', 'updatePage.html'), 'utf-8');
const myNotes = fs.readFileSync(path.join(__dirname, 'view', 'myNotes.html'), 'utf-8');
const updateMynotes = fs.readFileSync(path.join(__dirname, 'view', 'updateMynotes.html'), 'utf-8');
const noteDB = fs.readFileSync(path.join(__dirname, 'model', 'noteDB.json'), 'utf-8')
const dataDB = JSON.parse(noteDB);
const PORT = 8000;
const server = http.createServer((req, res) => {
const { query, pathname } = url.parse(req.url, true);
if (pathname == '/' && req.method == 'GET') {
res.writeHead(200, { 'content-type': 'text/html' });
const notes = dataDB.map(res => replaceTemplete(myNotes, res)).join('');
const indexRoute = indexPage.replace("{%myNotes%}", notes)
res.end(indexRoute)
} else if (pathname === '/add' && req.method === 'POST') {
const form = new formidable.IncomingForm();
form.parse(req, (err, fields, files) => {
if (err) {
res.writeHead(500, { 'content-type': 'text/plain' });
res.write(`<h1>${err.message}</h1>`);
res.end();
} else {
if (fields == undefined) {
res.writeHead(500, { 'content-type': 'text/plain' });
res.write(`<h1>fields is empty h1>`);
res.end();
} else {
const id = Math.floor(Math.random() * 1000) + 1;
const title = fields.title.toString();
const description = fields.description.toString();
dataDB.push({
id: id,
title: title,
description: description
})
console.log(dataDB)
fs.writeFile('./model/noteDB.json', JSON.stringify(dataDB, null, 2), (err) => {
if (err) {
res.writeHead(404, { 'content-type': 'text/plain' });
res.write(`<h1>${err.message}</h1>`);
res.end();
} else {
console.log('successfully added');
res.writeHead(302, { 'Location': '/' });
res.end();
}
})
res.writeHead(302, { 'Location': '/' });
res.end();
}
}
})
} else if (pathname == '/delete') {
const id = query.id;
const note = dataDB.filter(res => res.id !== Number(id))
console.log(id);
fs.writeFile('./model/noteDB.json', JSON.stringify(note, null, 2), (err) => {
if (err) {
res.writeHead(404, { 'content-type': 'text/plain' });
res.write(`<h1>${err.message}</h1>`);
res.end();
}
})
res.writeHead(302, { 'Location': '/' });
res.end();
} else if (pathname == '/search' && req.method == 'POST') {
const form = new formidable.IncomingForm();
form.parse(req, (err, fields, files) => {
if (err) {
res.writeHead(500, { 'content-type': 'text/plain' });
res.write(`<h1>${err.message}</h1>`);
res.end();
} else {
const setSearchValue = fields.search.toString();
const geSearchValue = dataDB.find(res => res.title.toLowerCase() == setSearchValue.toLowerCase())
console.log('geSearchValue', geSearchValue)
res.writeHead(200, { 'content-type': 'text/html' });
const notes = [geSearchValue].map(res => replaceTemplete(myNotes, res)).join('');
const indexRoute = indexPage.replace("{%myNotes%}", notes)
res.end(indexRoute)
}
});
} else if (pathname == '/updatePage') {
const updateId = query.id;
const updateNote = dataDB.find(res => res.id == Number(updateId));
res.writeHead(200, { 'content-type': 'text/html' });
const takenotes = [updateNote].map(res => replaceTemplete(updateMynotes, res)).join('');
const indexRoute = updatePage.replace("{%updateMynotes%}", takenotes)
res.end(indexRoute)
res.writeHead(302, { 'Location': '/' });
res.end();
} else if (pathname == '/update' && req.method == 'POST') {
const form = new formidable.IncomingForm();
form.parse(req, (err, fields, files) => {
if (err) {
console.log('Error during input fields..');
res.statusCode = 500;
res.writeHead(`error ${err.message}`);
res.end();
} else {
const id = fields.id.toString();
const title = fields.title.toString();
const description = fields.description.toString();
const updateNote = dataDB.map(res => {
if (res.id == Number(id)) {
return {
...res,
id: Number(id),
title: title,
description: description
}
}
return res;
})
fs.writeFile('./model/noteDB.json', JSON.stringify(updateNote), (err) => {
if (err) {
console.log('Error during file writing..');
res.statusCode = 500;
res.end();
} else {
console.log('successfully updated..');
res.writeHead(302, { 'Location': '/' });
res.end();
}
})
res.writeHead(302, { 'Location': '/' });
res.end();
}
});
} else {
res.writeHead(404, { 'content-type': 'text/html' });
res.write('<h1>OPPS! 404 page not found</h1>')
res.end();
}
})
server.listen(PORT, () => {
console.log(`listening on port ${PORT}`);
})