-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
193 lines (179 loc) · 6.18 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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
const http = require("http");
const path = require("path");
const fs = require("fs");
const server=http.createServer( function(req, res){
//path-ul fisierului cerut
var filePath=path.join(__dirname, "public", req.url === "/" ? "index.html" : req.url);
//determin ce tip de fisier mi se cere in contentType
var contentType="";
let extName=path.extname(filePath);
switch (extName) {
case ".js":
contentType = "text/javascript";
break;
case ".css":
contentType = "text/css";
break;
case ".json":
contentType = "application/json";
break;
case ".png":
contentType = "image/png";
break;
case ".ico":
contentType = "image/ico";
break;
case ".jpg":
contentType = "image/jpg";
break;
}
//tratez cazurile in functie de metoda
if (req.method === "POST") {
if (contentType === "application/json") {
handleApiPostRequests (req, res, filePath);
}
res.writeHead(200, {"Content-Type": contentType});
} else if (req.method === "PUT") {
if (contentType === "application/json") {
handleApiPutRequests (req, res, filePath);
}
res.writeHead(200, {"Content-Type": contentType});
} else if (req.method === "DELETE") {
if (contentType === "application/json") {
handleApiDeleteRequests (req, res, filePath);
}
res.writeHead(200, {"Content-Type": contentType});
} else if (req.method === "GET") {
//o fac dinamic daca mai am timp
/* if (path.basename === "user.html") {
//asta e o pagina pe care vreau s-o generez dinamic inainte de servire
var content="";
//asta am de facut^
fs.writeFile(filePath, content,function(err) {
});
} */
fs.readFile(filePath, function(err, content) {
res.end(content);
});
res.writeHead(200, {"Content-Type": contentType});
}
});
//scrie ceea ce se primeste prin req la filePath (cu fs.appendFile)
//trimite in response obiectul scris
//poate sa sterg parola de la astea, desi asta ar face api-ul specific
function handleApiPostRequests (req, res, filePath) {
//primesc ce-am de primit
var primit="";
req.on("data", function(received) {
primit += received;
});
req.on("end", function() {
var jsonToAppend=JSON.parse(primit);
fs.readFile(filePath, function(err, content) {
//content e ce am in api pana acum;
var currentJson=JSON.parse(content);
//imi iau contentul si adaug la el nodul primit;
if (Array.isArray(currentJson)) {
jsonToAppend["id"]=currentJson.length+1;
currentJson.push(jsonToAppend);
} else {
jsonToAppend["id"]=1; //e primul user daca nu mai e nici unul
currentJson=[jsonToAppend]; //ma asigur ca-l fac array;
}
fs.writeFile(filePath, JSON.stringify(currentJson), function(err) {
console.log("eroare");
});
res.end(JSON.stringify("S-a introdus cu succes."));
});
});
}
function handleApiPutRequests (req, res, filePath) {
//primesc ce-am de primit
var primit="";
req.on("data", function(received) {
primit += received;
});
req.on("end", function() {
var jsonToUpdate=JSON.parse(primit);
fs.readFile(filePath, function(err, content) {
var negasit=false;
//content e ce am in api pana acum;
var currentJson=JSON.parse(content);
//imi iau contentul si adaug la el nodul primit;
if (Array.isArray(currentJson)) {
var append=true;
var i=0;
while(append) { //caut obiectul si ii schimb pe ala potrivit
if (currentJson[i].id == jsonToUpdate.id) {
currentJson[i]=jsonToUpdate;
append=false;
}
i++;
}
if (i=== currentJson.length) {negasit=true;}
} else if (currentJson) {
if (currentJson.id != jsonToUpdate.id) {
negasit=true;
} else {
currentJson=jsonToUpdate;
}
} else {negasit=true;}
if (!negasit) {
fs.writeFile(filePath, JSON.stringify(currentJson), function(err) {
console.log("eroare");
});
res.end("S-a updatat cu succes");
} else {
res.end("Nu a fost gasit obiectul care se dorea updatat.");
}
});
});
}
function handleApiDeleteRequests (req, res, filePath) {
//primesc ce-am de primit
var primit="";
req.on("data", function(received) {
primit += received;
});
req.on("end", function() {
var jsonToDelete=JSON.parse(primit);
fs.readFile(filePath, function(err, content) {
var negasit=false;
//content e ce am in api pana acum;
var currentJson=JSON.parse(content);
//imi iau contentul si adaug la el nodul primit;
if (Array.isArray(currentJson)) {
var sterge=true;
var i=0;
while(sterge) { //caut obiectul si il sterg
if (currentJson[i].id == jsonToDelete.id) {
currentJson.splice(i,1); //sterge componenta de la poz i
//updeateaza id-urile tuturor celorlalte componente
for (var j=i; j<currentJson.length; j++) {
currentJson[j].id--;
}
sterge=false;
}
i++;
}
if (i=== currentJson.length) {negasit=true;}
} else if (currentJson) {
if (currentJson.id != jsonToDelete.id) {
negasit=true;
} else {
currentJson={};
}
} else {negasit=true;}
if (!negasit) {
fs.writeFile(filePath, JSON.stringify(currentJson), function(err) {
console.log("eroare");
});
res.end("S-a sters cu succes");
} else {
res.end("Nu a fost gasit obiectul care se dorea sters.");
}
});
});
}
const PORT = process.env.port || 5000;
server.listen( PORT, function () {console.log("Serverul ruleaza pe portul", PORT);});