-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram.js
More file actions
410 lines (241 loc) · 10.5 KB
/
program.js
File metadata and controls
410 lines (241 loc) · 10.5 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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
//////////////////////////////////////////
/* this program contains exercises from */
/* freecodecamps nodejs tutorials */
//////////////////////////////////////////
//////////////////
///// START /////
//////////////////
/*////////////////////////////////*/
/* function baby steps exercise 2 */
/*////////////////////////////////*/
function getSum(){
var sum = 0;
for(var i=2;i<process.argv.length;i++){ // loop through arguments and add them up
sum+= Number(process.argv[i]); // arguments start from index 2
}
console.log(sum);
}
//getSum();
/////////////////////////////////////////
/* function exercise 3 MY FIRST I/O */
/////////////////////////////////////////
function syncIO(){
var fs = require('fs'); // load file system module
var fFile = process.argv[2]; // read file path from args
var bufferObject = fs.readFileSync(fFile); // read file contents to the buffer
var strString = bufferObject.toString(); // convert file contents or the buffer object to string
var arrArray = strString.split('\n'); // divide new lines to array elements
var count = arrArray.length - 1; // count number of lines, note the last line doesnt end with new line i.e '\n'
console.log(count);
}
//syncIO();
////////////////////////////////////////////////
/* function exercise 4 MY FIRST ASYNC I/O! */
////////////////////////////////////////////////
function asyncIO(){
var fs = require('fs'); // load file system module
var fFile = process.argv[2]; // read file path from args
fs.readFile(fFile, function doneReading(err, fileContents) { // readFile is asycronous so a call back function is passed.
if(!err){
var strString = fileContents.toString(); // convert file contents or the buffer object to string
var arrArray = strString.split('\n'); // divide new lines to array elements
var count = arrArray.length - 1; // count number of lines, note the last line doesnt end with new line i.e '\n'
console.log(count);
}
});
}
//asyncIO();
//////////////////////////////////////////////
/* function exercise 5 FILTERED LS */
//////////////////////////////////////////////
function filterLS(){
var fs = require('fs'); // load file system module
var pa = require('path'); // load path module
var ex = process.argv[3]; // filename extension
var fPath = process.argv[2]; // read file path from args
fs.readdir(fPath, function doneReading(err, arrList) { // readFile is asycronous so a call back function is passed.
if(!err){
arrList.forEach(function (item){ // loop through filenames
if (pa.extname(item) === '.' + ex) { // check if the filename extension is same as the passed in extension.
console.log(item); // Print the filename.
}
});
}
});
}
//filterLS();
//////////////////////////////////////////////
/* function exercise 6 NODE MODULES */
//////////////////////////////////////////////
function nodeModule(){
var mymodule = require('./findFiles.js');
var ex = process.argv[3]; // filename extension
var fPath = process.argv[2]; // read file path from args
mymodule(fPath,ex,function (err, arrList) {
if (err)
return console.error('There was an error:', err);
arrList.forEach(function (item){ // loop through filenames
console.log(item); // Print the filename.
});
});
}
//nodeModule();
//////////////////////////////////////////////
/* function exercise 7 HTTP CLIENT */
//////////////////////////////////////////////
function httpClient(){
var http = require('http'); // load http module
var url = process.argv[2]; // get url from args
http.get(url, function(response) {
response.setEncoding('utf8'); // the "data" events will emit Strings rather than the standard Node Buffer objects
response.on("data", function(data) {
console.log(data); // Print data
});
});
}
//httpClient();
//////////////////////////////////////////////
/* function exercise 8 HTTP COLLECT */
//////////////////////////////////////////////
//npm install bl
function httpCollect(){
var http = require('http'); // load http module
var bl = require('bl'); // load bl module
var url = process.argv[2]; // get url from args
http.get(url, function(response) {
response.pipe(bl(function (err, data) {
if(!err){
var strString = data.toString(); // convert the buffer object to string
console.log(data.length); // Print length
console.log(strString); // Print data
}
}));
});
}
//httpCollect();
//////////////////////////////////////////////
/* function exercise 9 JUGGLING ASYNC */
//////////////////////////////////////////////
//npm install bl
function httpAsync(){
var http = require('http'); // load http module
var bl = require('bl'); // load bl module
var urls = [];
urls.push(process.argv[2]); // get url 1 from args
urls.push(process.argv[3]); // get url 2 from args
urls.push(process.argv[4]); // get url 3 from args
var res = []; // responses recieved.
var count = 0; // count number of responses.
urls.forEach(function(url, index){
http.get(url, function(response) {
response.pipe(bl(function (err, data) {
if(!err){
var strString = data.toString(); // convert the buffer object to string
res[index] = strString;
count++;
if(count==3){
res.forEach(function(item){
console.log(item); // Print data
});
}
}
}));
});
});
}
//httpAsync();
//////////////////////////////////////////////
/* function exercise 10 TIME SERVER */
//////////////////////////////////////////////
function tcpTime(){
var net = require('net'); // load networking module
var port = process.argv[2]; // get the port number from args
var server = net.createServer(function (socket) {
// socket handling logic
var date = new Date();
var yy = date.getFullYear();
var mm = date.getMonth(); // starts at 0
if(mm<9)mm = '0'+ (mm+1);
var dd = date.getDate(); // returns the day of month
if(dd<10)dd = '0'+ dd;
var hh = date.getHours();
if(hh<10)hh = '0'+ hh;
var min = date.getMinutes();
if(min<10)min = '0'+ min;
var data = yy + '-' + mm + '-' + dd + ' ' +hh+':'+min;
socket.write(data+'\n');
socket.end();
});
server.listen(port);
}
//tcpTime();
//////////////////////////////////////////////
/* function exercise 11 FILE SERVER */
//////////////////////////////////////////////
function httpFile(){
var http = require('http'); // load networking module
var port = process.argv[2]; // get the port number from args
var fPath = process.argv[3]; // read file path from args
var fs = require('fs'); // load file system module
var server = http.createServer(function (req, res) {
// socket handling logic
var content = fs.createReadStream(fPath); // read incoming file stream
content.pipe(res); // write out
});
server.listen(port);
}
//httpFile();
//////////////////////////////////////////////
/* function exercise 12 Uppercaserer */
//////////////////////////////////////////////
function httpUPPER(){
var http = require('http'); // load networking module
var port = process.argv[2]; // get the port number from args
var map = require('through2-map'); // load map module
var server = http.createServer(function (inStream, outStream) {
// socket handling logic
inStream.pipe(map(function (chunk) {
return chunk.toString().toUpperCase();
})).pipe(outStream);
});
server.listen(port);
}
//httpUPPER();
//////////////////////////////////////////////
/* function exercise 13 JSON API SERVER */
//////////////////////////////////////////////
function httpAPI(){
var http = require('http'); // load networking module
var port = process.argv[2]; // get the port number from args
var url = require('url'); // load url module
var result = '';
var server = http.createServer(function (inStream, outStream) {
// socket handling logic
var parsedUrl = url.parse(inStream.url, true); // divede url into parts.
var iso = parsedUrl.query.iso; // iso time
var format = parsedUrl.pathname; // convert into date format
var time = new Date(iso); // time object
console.log(parsedUrl);
if(format=="/api/parsetime"){ // iso format to date
result = {
hour: time.getHours(),
minute: time.getMinutes(),
second: time.getSeconds()
};
}
if(format=="/api/unixtime"){ // iso format to unix datetime
result = {
unixtime : time.getTime()
};
}
if(Object.keys(result).length>0){ // count objects
outStream.writeHead(200, { 'Content-Type': 'application/json' });
outStream.end(JSON.stringify(result)); // write the json to output
}else{
outStream.writeHead(404);
outStream.end();
}
});
server.listen(port);
}
httpAPI();