-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
632 lines (484 loc) · 19.2 KB
/
app.js
File metadata and controls
632 lines (484 loc) · 19.2 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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
// ------------------------------------------------------
// --- say hello app
// ------------------------------------------------------
// function sayHello(name) {
// console.log("Hello " + name);
// }
// sayHello("bryan");
// console.log(window);
// ------------------------------------------------------
// --- modules
// ------------------------------------------------------
// In node, every file is module
// when you console.log(module), you get a json object of the module
// we don't want to accidentally override the value of logger so we define it as a constant
// const log = require("./logger.js");
// log("message");
// look into jshint
// ------------------------------------------------------
// --- path
// ------------------------------------------------------
// this method of requiring by using "path" gives you an object with the current folder instead of having to list a string of the desired file path
// const path = require("path");
// var pathObj = path.parse(__filename);
// console.log(pathObj);
// ------------------------------------------------------
// --- operating system
// ------------------------------------------------------
// const os = require("os");
// var totalMemory = os.totalmem();
// var freeMemory = os.freemem();
// console.log("Total Memory: " + totalMemory);
// console.log("Free Memory: " + freeMemory);
// ------------------------------------------------------
// --- template string
// ------------------------------------------------------
// Template String (elimiates need to use concatinating syntax)
// ES6 / ES2015
// console.log(`Total Memory: ${totalMemory}`);
// console.log(`Free Memory: ${freeMemory}`);
// ------------------------------------------------------
// --- fs
// ------------------------------------------------------
// const fs = require("fs");
// const files = fs.readdirSync("./");
// console.log(files);
// ------------------------------------------------------
// --- fs asynchronus
// ------------------------------------------------------
// fs.readdir("./", function(err, files) {
// if (err) console.log("Error", err);
// else console.log("Result", files);
// });
// ------------------------------------------------------
// --- Event Emitter
// ------------------------------------------------------
// the name of the class "EventEmitter" includes a capitalized first letter and is not "eventEmitter" becuase it sets it apart as a class
// CLASS: A class is a container for multiple related methods and properties
// CLASS vs OBJECT: Class = Human, Object = John (an actual individual instance of the class)
// logger for event listeners, classes, etc
// const EventEmitter = require('events');
// const Logger = require("./logger");
// const logger = new Logger();
// register a listener - ES6 Function
// logger.on("messageLogged", (event) => {
// console.log("listener called", event);
// });
// logger.log("message");
// ------------------------------------------------------
// --- http, server
// ------------------------------------------------------
// const http = require("http");
// const hostname = "127.0.0.1";
// const port = 3001;
// const server = http.createServer((req, res) => {
// if (req.url === "/") {
// res.statusCode = 200;
// res.setHeader("Content-Type", "text/plain");
// res.end("Hello World!\nWhat the eff is up?\n");
// }
// if (req.url === "/api/courses") {
// res.write(JSON.stringify([1, 2, 3]));
// res.end();
// }
// });
// server.listen(port, hostname, () => {
// console.log(`Server running at ${port}:${hostname}/`);
// });
// ------------------------------------------------------
// --- more fs
// ------------------------------------------------------
// const fs = require("fs");
// ------------------------------------------------------
// --- append
// ------------------------------------------------------
// fs.appendFile(
// "file.txt",
// "hello world.\nwhat the eff is up y'all?\nlet's party.\n",
// (err) => {
// if (err) throw err;
// console.log("New text appended to the file!");
// });
// ------------------------------------------------------
// --- open file and append
// ------------------------------------------------------
// fs.open("file.txt", "a", (err, fd) => {
// if (err) throw err;
// fs.appendFile(fd, "text/data to append", "utf8", (err) => {
// fs.close(fd, (err) => {
// if (err) throw err;
// });
// if (err) throw err;
// })
// console.log("New text appended to the file!");
// });
// ------------------------------------------------------
// --- rename and delete
// ------------------------------------------------------
// fs.rename("/home/filename1", "/home/newfilename1", (err) => {
// if (err) throw err;
// console.log("rename completed");
// });
// delete is fs.unlink();
// ------------------------------------------------------
// --- more path
// ------------------------------------------------------
// basename - get current path
// const path = require("path");
// const myPath = path.basename(__filename);
// console.log(myPath);
// const baz = path.basename("/foo/bar/baz.html");
// console.log(baz); // baz.html
// const baz2 = path.basename("/foo/bar/baz.html", ".html");
// console.log(baz2);
// use path.join to create a unique file path to serve an individualized page based upon a user name or user activity
// let userName = "David";
// const join = path.join("/users", userName, "index.html");
// console.log(join);
// consistent paths in windows - path.win32.basename()
// consistent paths in both windows and linux - path.posix.basename();
// ------------------------------------------------------
// --- more event listeners
// ------------------------------------------------------
// const {EventEmitter} = require("events");
// const eventEmitter = new EventEmitter();
// listens for the event and does something when that event happens
// eventEmitter.on("SoloLearn", () => {
// console.log("SoloLearn occurred!");
// })
// fires the event
// eventEmitter.emit("SoloLearn");
// more than one event listener on the eventEmitter object
// eventEmitter.on("mySecondEvent", () => {
// console.log("mySecondEvent occured!");
// });
// eventEmitter.emit("mySecondEvent");
// add parameters to the event listener
// eventEmitter.on("Person", (name, age) => {
// console.log(`I'm ${name} and I'm ${age} years old`);
// })
// emit person with parameters
// eventEmitter.emit("Person", "David", "30");
// eventNames shows all of the events attached to an emitter
// console.log(eventEmitter.eventNames());
// ------------------------------------------------------
// --- timers
// ------------------------------------------------------
// setImmediate - runs code once immediately
// setImmediate((name, age) => {
// // usually the heavy operations go here
// console.log(`I'm ${name} and I'm ${age} years old`)
// }, "David", 28);
// setInterval - runs the code every interval set - every 2 seconds in this case
// setInterval((name, age) => {
// console.log(`I'm ${name} and I'm ${age} years old`)
// }, 2000, "David", 28);
// setTimeout - runs the code once after the time set - every 2 seconds in this case
// setTimeout((name, age) => {
// console.log(`I'm ${name} and I'm ${age} years old`)
// }, 2000, "David", 28);
// ------------------------------------------------------
// --- cancelling timers
// ------------------------------------------------------
// clearImmediate(immediate);
// clearInterval(timeout);
// clearTimeout(timeout);
// clearing example
// generates and logs a random number every second as long as the number is under 10. If the random number generated is greater than 8, the clearInterval runs and the timer is cancelled.
// const myTime = setInterval(() => {
// const num = Math.floor((Math.random() * 10) + 1);
// console.log("the random number is: " + num);
// if (num > 8) {
// clearInterval(myTime);
// }
// }, 1000);
// ------------------------------------------------------
// --- streams
// ------------------------------------------------------
// streams are good for large amounts of data - similar to strings or arrays accept that streams might not be available all at once (memory size or bandwidth issues). Streams load data in chunks once at a time. Handling big data is not the only use case for streams though. You can also use them for piping the data into other commands (similar to piping in Linux).
// Example: req and res in node.js server creation are streams
// req is an http.IncomingMessage, which is a Readable Stream
// res is an http.ServerResponse, which is a Writable Stream
// fs.createWriteStream() method creates a writable stream
// fs.createReadStream() method creates a readable stream
// readable streams use the EventEmitter to notify application code when data is availalbe to be read from the stream. The available data can be ready from the stream in multiple ways.
// writes the specified text to the file one million times - a big writable file
// const fs = require("fs");
// const file = fs.createWriteStream("./big-file.txt");
// for (let i = 0; i <= 1000000; i++) {
// file.write("Lorem ipsum dolor sit amet consectetur adipisicing elit. Tempora provident aut voluptates ipsum labore. Ullam hic perspiciatis earum pariatur, vero itaque similique nobis inventore! Eaque quibusdam voluptates iusto obcaecati sapiente.\n");
// }
// file.end();
// serve the big file from a node.js server - the readFile method loads the entirefile into RAM before responding
// const server = require("http").createServer();
// const fs = require("fs");
// server.on("request", (req, res) => {
// fs.readFile("./big-file.txt", (err, data) => {
// if (err) throw err;
// res.end(data);
// });
// });
// server.listen(3001);
// the createReadStream() method "pipes" the stream to the response object in chunks instead of waiting for the entire response to load into RAM
// const server = require("http").createServer();
// const fs = require("fs");
// server.on("request", (req, res) => {
// const src = fs.createReadStream("./big-file.txt");
// src.pipe(res);
// });
// server.listen(3001);
// ------------------------------------------------------
// --- read data from a file and log it
// ------------------------------------------------------
// var fs = require("fs");
// fs.readFile("Unsolved/best_things_ever.txt", "utf8", (error, data) => {
// if (error) throw err;
// var dataArr = data.split(",");
// dataArr.forEach((item) => {
// console.log(item);
// })
// });
// ------------------------------------------------------
// --- npm and http requests
// ------------------------------------------------------
// npm init will create a package.json
// npm i will install packages
// var request = require("request");
// request("https://en.wikipedia.org/wiki/Kudos_(granola_bar)", function(error, response, body) {
// if (!error && response.statusCode === 200) {
// console.log(body);
// }
// });
// ------------------------------------------------------
// --- learning packages and requests with OMDB
// ------------------------------------------------------
// var request = require("request");
// var nodeArgs = process.argv;
// var movieName = "";
// for (var i = 2; i < nodeArgs.length; i++) {
// if (i > 2 && i < nodeArgs.length) {
// movieName = movieName + "+" + nodeArgs[i];
// }
// else {
// movieName += nodeArgs[i];
// }
// }
// var queryUrl = "http://www.omdbapi.com/?t=" + movieName + "&y=&plot=short&apikey=trilogy";
// request(queryUrl, function(error, response, body) {
// if (!error && response.statusCode === 200) {
// var data = JSON.parse(body);
// console.log("The movie's title is: " + data.Title);
// console.log("The movie's IMDB rating is: " + data.imdbRating);
// console.log("The movie is rated " + data.Rated);
// console.log("The movie's release year is: " + data.Year);
// }
// });
// ------------------------------------------------------
// --- node bank acct
// ------------------------------------------------------
// var input = process.argv[3];
// var operator = process.argv[2];
// function getTotal() {
// fs.readFile("bank.txt", "utf8", (error, data) => {
// if (error) throw err;
// else
// total = 0;
// var dataArr = data.split(",");
// for (let i = 0; i < dataArr.length-1; i++) {
// total += parseFloat(dataArr[i]);
// }
// console.log(total);
// });
// }
// const fs = require("fs");
// switch(operator) {
// case "total":
// getTotal();
// break;
// case "deposit":
// fs.appendFile("bank.txt", `${input},`, (err) => {
// if (err) console.log(err);
// else getTotal();
// });
// break;
// case "withdraw":
// fs.appendFile("bank.txt", `-${input},`, (err) => {
// if (err) console.log(err);
// else getTotal();
// });
// break;
// case "lotto":
// const num = Math.floor((Math.random() * 10) + 1);
// console.log(`The lucky number is ${num}`);
// if (parseFloat(input) === num) {
// fs.appendFile("bank.txt", `${input*2},`, (err) => {
// if (err) console.log(err);
// else getTotal();
// });
// break;
// }
// else {
// fs.appendFile("bank.txt", `-${input},`, (err) => {
// if (err) console.log(err);
// else getTotal();
// });
// break;
// }
// }
// ------------------------------------------------------
// --- Geocode locations
// ------------------------------------------------------
// var NodeGeocoder = require("node-geocoder");
// var options = {
// provider: "mapquest",
// apiKey: "AOwMupgiBjoRn6lUvtkSYTs86mcuAJaK"
// };
// var geocoder = NodeGeocoder(options);
// var nodeArgs = process.argv;
// var location = "";
// for (var i = 2; i < nodeArgs.length; i++) {
// if (i > 2 && i < nodeArgs.length) {
// location = location + "+" + nodeArgs[i];
// }
// else {
// location += nodeArgs[i];
// }
// }
// console.log(location);
// geocoder.geocode(location, function(err, res) {
// console.log(JSON.stringify(res[0], null, 2));
// if (err) throw err;
// });
// ------------------------------------------------------
// --- Geocoding with Weather
// ------------------------------------------------------
// Include both the weather and node-geocoder NPM packages
// var weather = require("weather-js");
// var NodeGeocoder = require("node-geocoder");
// // Replace with your mapquest consumer API key
// var options = {
// provider: "mapquest",
// apiKey: "AOwMupgiBjoRn6lUvtkSYTs86mcuAJaK"
// };
// var geocoder = NodeGeocoder(options);
// Get all elements in process.argv, starting from index 2 to the end
// Join them into a string to get the space delimited address
// var address = process.argv.slice(2).join(" ");
// Then use the Google Geocoder to geocode the address
// geocoder.geocode(address, function(err, data) {
// console.log(JSON.stringify(data[0], null, 2));
// var address = data[0];
// Depending on what information is available for an address, build formatted search
// var search = "";
// if (address.city) {
// search += address.city;
// }
// if (address.stateCode) {
// search += ", " + address.stateCode;
// }
// if (address.zipcode) {
// search += " " + address.zipcode;
// }
// if (address.countryCode) {
// search += " " + address.countryCode;
// }
// Run the weather package to search by either zip or city.
// weather.find({ search: search, degreeType: "F" }, function(err, result) {
// If there is insufficient data, notify the user.
// if (err) {
// console.log("\r\n\r\n\r\n");
// console.log("Sorry we don't have enough data on that location! Try somewhere else.");
// console.log("\r\n\r\n\r\n");
// return;
// }
// Then print the Weather information and complete Address
// console.log("\r\n\r\n\r\n");
// console.log("Weather Forecast for: " + search);
// console.log("Current Temperature: " + result[0].current.temperature + "° F");
// console.log("Sky: " + result[0].current.skytext);
// console.log(
// "Tomorrow's Forecast: Low of " +
// result[0].forecast[1].low +
// "° F | High of " +
// result[0].forecast[1].high +
// "° F"
// );
// console.log("\r\n\r\n\r\n");
// });
// });
// ------------------------------------------------------
// --- get responses to pre-set q's inquirer package
// ------------------------------------------------------
// Load the NPM Package inquirer
// var inquirer = require("inquirer");
// Create a "Prompt" with a series of questions.
// inquirer
// .prompt([
// // Here we create a basic text prompt.
// {
// type: "input",
// message: "What is your name?",
// name: "username"
// },
// // Here we create a basic password-protected text prompt.
// {
// type: "password",
// message: "Set your password",
// name: "password"
// },
// // Here we give the user a list to choose from.
// {
// type: "list",
// message: "Which Pokemon do you choose?",
// choices: ["Bulbasaur", "Squirtle", "Charmander"],
// name: "pokemon"
// },
// // Here we ask the user to confirm.
// {
// type: "confirm",
// message: "Are you sure:",
// name: "confirm",
// default: true
// }
// ])
// .then(function(inquirerResponse) {
// // If the inquirerResponse confirms, we displays the inquirerResponse's username and pokemon from the answers.
// if (inquirerResponse.confirm) {
// console.log("\nWelcome " + inquirerResponse.username);
// console.log("Your " + inquirerResponse.pokemon + " is ready for battle!\n");
// }
// else {
// console.log("\nThat's okay " + inquirerResponse.username + ", come again when you are more sure.\n");
// }
// });
// ------------------------------------------------------
// --- inquirer and geocoding with user input
// ------------------------------------------------------
// var inquirer = require("inquirer");
// var NodeGeocoder = require("node-geocoder");
// var options = {
// provider: "mapquest",
// apiKey: "AOwMupgiBjoRn6lUvtkSYTs86mcuAJaK"
// };
// var geocoder = NodeGeocoder(options);
// inquirer.prompt([
// {
// type: "input",
// message: "Enter a location to get coordinates.",
// name: "location"
// }
// ]).then(function(inquirerResponse) {
// if (inquirerResponse.location) {
// address = inquirerResponse.location;
// // Then use the Google Geocoder to geocode the address
// geocoder.geocode(address, function(err, data) {
// // Then console log the result and stringify it.
// // Note the argument of "2" being included in the JSON stringify. This makes the JSON output pretty.
// // See link here: http://stackoverflow.com/questions/4810841/how-can-i-pretty-print-json-using-javascript
// console.log(JSON.stringify(data[0].city, null, 2));
// console.log(JSON.stringify(data[0].state, null, 2));
// console.log(JSON.stringify(data[0].latitude, null, 2));
// console.log(JSON.stringify(data[0].longitude, null, 2));
// });
// }
// });