-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequestHandlers.js
265 lines (199 loc) · 6.88 KB
/
requestHandlers.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*var fs = require('fs');
function start(response) {
console.log("Request handler 'start' was called.");
fs.readFile('./index.html', function (err, html) {
if (err) {
throw err;
}
else
response.writeHead(200, {"Content-Type": "text/html"});
response.write(html);
response.end();
});
}
function upload(response) {
console.log("Request handler 'upload' was called.");
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello Upload");
response.end();
}
exports.start = start;
exports.upload = upload;
*/
var fs = require('fs');
var url = require('url');
var querystring = require("querystring");
var obj;
function start(response/*, postData*/) {
console.log("Request handler 'start' was called.");
fs.readFile('./page.html', function (err, data) {
if (err) throw err;
response.writeHead(200, {"Content-Type": "text/html"});
response.write(data);
response.end();
});
fs.readFile('./file.json', function(err,data) {
if (err) throw err;
obj = JSON.parse(data);
})
/*fs.readFile('./file.json', function (err, data) {
if (err) throw err;
obj = JSON.parse(data);
obj.splice( postData,1);
var fs = require('fs');
fs.writeFile("./file.json",JSON.stringify(obj),function (err) {
if(err) {
return console.log(err);
//response.writeHead(409);
response.end();
}
console.log("The file was saved!");
response.writeHead(200);
response.end();
// writeHead(301,{Location: '/'});response.end();
});
});*/
/*
var body = '<html>'+
'<head>'+
'<meta http-equiv="Content-Type" content="text/html; '+
'charset=UTF-8" />'+
'</head>'+
'<body>'+
'<form action="/sendtext" method="post">'+
'<input type="text" name="q" placeholder="Поиск" value="">'+
'<textarea name="text" rows="20" cols="20"></textarea>'+
'<textarea name="text2" rows="20" cols="20"></textarea>'+
'<input type="checkbox" name="checkbox" >Показывать' +
'<input type="submit" value="Submit text" />'+
'</form>'+
'<table>';
fs.readFile('./file.json', function (err, data) {
if (err) throw err;
var obj = JSON.parse(data);
for (var i=0;i<obj.length;i++)
{
body=body+
'<tr>' +
'<td class="service">'+obj[i].service.toString()+'</td>'+
'<td class="contractor">'+obj[i].contractor.toString()+'</td>'+
'<td class="actions">' +
// '<a title="Информация" href="/infoCity?id=1">🛈</a>' +
//'<a title="Изменить" href="/editCity?id=1">✎</a>' +
"<a title=\"Удалить\" href=\"/removeService?id=" + i +"\""+'>✖</a>' +
'</td>'+
'</tr>';
//body= body + obj[i].service.toString()+obj[i].contractor.toString();
}
body=body+
'</table>'+
'</body>'+
'</html>';
response.writeHead(200, {"Content-Type": "text/html"});
response.write(body);
response.end();
});*/
}
function sendjquery(response) {
console.log("Request handler 'jquery' was called.");
fs.readFile('./js/jquery.js', function (err, data) {
if (err) throw err;
response.writeHead(200, {"Content-Type": "text/js"});
response.write(data);
response.end();
})
}
function upload(response, postData) {
console.log("Request handler 'upload' was called.");
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("You've sent the text: "+
querystring.parse(postData).text);
response.end();
}
function sendtext(response, postData) {
console.log("Request handler 'send' was called.");
fs.readFile('./index.html', function (err, html) {
if (err) {
throw err;
}
response.writeHead(200, {"Content-Type": "text/html"});
response.write(html+"You've sent : "+ querystring.parse(postData).text + querystring.parse(postData).text2+ querystring.parse(postData).q+querystring.parse(postData).checkbox);
response.end();
})}
function removeService(response,postData, request) {
console.log("Request handler 'upload' was called.");
/* response.writeHead(200, {"Content-Type": "text/html"});
response.write( querystring.parse(que).id);
response.end();
*/
obj.splice(postData,1);
fs.writeFile("./file.json",JSON.stringify(obj),function (err) {
if (err) throw err;
console.log("The file was saved!");
response.writeHead(200);
response.end();
})
/*fs.readFile('./file.json', function (err, data) {
if (err) throw err;
obj = JSON.parse(data);
obj.splice( postData,1);
var fs = require('fs');
fs.writeFile("./file.json",JSON.stringify(obj),function (err) {
if(err) {
return console.log(err);
//response.writeHead(409);
response.end();
}
console.log("The file was saved!");
response.writeHead(200);
response.end();
// writeHead(301,{Location: '/'});response.end();
});
});*/
}
/*
function removeService(response,postData, request) {
console.log("Request handler 'upload' was called.");
var obj;
var que = url.parse(request.url).query;que
if(querystring.parse(que).id) {
/* response.writeHead(200, {"Content-Type": "text/html"});
response.write( querystring.parse(que).id);
response.end();
*\/
fs.readFile('./file.json', function (err, data) {
if (err) throw err;
obj = JSON.parse(data);
obj.splice( querystring.parse(que).id,1);
var fs = require('fs');
fs.writeFile("./file.json",JSON.stringify(obj),function (err) {
if(err) {
return console.log(err);
}
console.log("The file was saved!");
response.writeHead(301,{Location: '/'});
response.end();
// writeHead(301,{Location: '/'});response.end();
});
//body= body + obj[i].service.toString()+obj[i].contractor.toString();
});
}
}
*/
function sendjson(response, postData) {
console.log("Request handler 'send' was called.");
fs.readFile('./file.json', function (err, html) {
if (err) {
throw err;
}
response.writeHead(200, {"Content-Type": "text/json"});
response.write(html);
response.end();
})
}
exports.start = start;
exports.upload = upload;
exports.sendtext = sendtext;
exports.removeService = removeService;
exports.sendjson = sendjson;
exports.sendjquery = sendjquery;