NodeJS



Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. Node.js' package ecosystem, npm, is the largest ecosystem of open source libraries in the world.

NPM Stuff - Node Package Manager

  1. npmjs.com - Essential JavaScript development tools that help you go to market faster and build powerful applications using modern open source code.
  2. Popular NPM Commands
  3. npm install npm@latest -g
  4. Favorite NPM Installs

Node applications are Asynchronous by default.

Locally and Globally install modules.



Udemy Nodejs Course

  1. Section 1: Welcome

  2. Section 2: Installing and Exploring Node.js

  3. Section 3: Node.js Module System (Notes App)

  4. Section 4: File System and Command Line Args
    1. 14. Section Intro: File System and Command Line Args
    2. 15. Getting Input from Users
      • In this lesson we're going to handle getting input from the user via command line arguments.
      • use clear to clear the terminal in VS Code terminal window connected to bash.
      • We have a process global variable that we can interact with.
      • The browser has window and document. Node has global and process we can interact with.
      • We are going to add console.log(process.argv) to our app.js file. Then run run node app.js Richard This stands for argument vector which in node stands for an array that contains all of the arguments provided.
      • The above statement returns an array as you see below. Now that this information is in an array we can interact with it.
      • [ 'C:\\Program Files\\nodejs\\node.exe', "C:\\Users\\Richard's Area 51\\Dropbox\\node-course\\notes-app\\app.js", 'Richard' ]
      • We had 3 strings return in this example. The first is the path the the node.exe file. The second is the path to our app.js file. The 3rd is the argument we appended to our command line statement.
      • We can specify which index in the array we want returned using console.log(process.argv[2])
      • Now we can use this argument to pass in a command or text to be processed.
      • In our case we are going to pass in a command that our app can act on.
      • Enter the following in our terminal.
      • node app.js add
      • Because we told process.argv to use the 3 item in the array, we have add console logged to the terminal.
      • Now we are going to create an in statement in our app.js file to conditionally run some code.
        • Add const command = process.argv[2] to our app.js file.
        • Add an if statement to check if the text add was entered on the command line.
        • if (command === 'add') { console.log(chalk.inverse.green('Yes, add was entered on the command line.')) }else { console.log(chalk.inverse.red('No, add was not entered on the command line.')) }
        • If someone is trying to add a note, we need the note title and the note body.
          • node app.js add --title="My First Title."
          • At this point the --title="My First Title." is not getting parsed.
          • Was the title provided? If yes, what was the value.
    3. 16. Argument Parsing with Yargs: Part I
      • Argument Parsing is not unique to our project so why not just find a npm package to handle argument parsing for us.
      • yargs is a great npm package to handle argument parsing so we are going to use it in our project.
      • Go to npmjs.com and search for yargs.
      • Install yargs into our project using the command line.
      • Next we need to require yargs on our app.js file.
      • const yargs = require('yargs')
      • console.log(yargs.argv) - Notice when you run this command you get key value pairs for the title data.
      • node app.js --help | This will show us the current options in our yargs configuration
      • // Customize yargs version
      • We do this by calling the version method on yargs: yargs.version('1.1.0')
      • At the command line: node app.js --version
      • Add commands to yargs: See my app.js file for the code.
      • Next I want to test that the add command option has been added. | At the command line I will type node app.js --help
      • Continue testing by entering the following at the command line. node app.js add
      • I copied the code for the add command and created the remove command.
      • Tested it using node app.js --help
    4. 17. Argument Parsing with Yargs: Part II
      • Next we adding builder: to the yargs.command.
      • Now I am switching to app5.js as my app file is getting clogged with old code.
      • For a reason I do not understand yet, the console.log(yargs.argv) statement is required. You have to access the yargs.argv in order to make it do its thing. Later will learn how to do this without using console.log.
      • In the builder section of the add command I need to add demandOption: true. This is make -- title a required field.
      • Next I need to make sure the user enters a string for the title.
      • type: 'string'
      • // I have replaced the statement above (console.log(yargs.argv)) with just
      • yargs.parse()
      • // Now it will do its thing without printing to the console.
      • Next I improved my add command handler function by changing the console.log to console.log('Title: ', argv.title)
      • Challenge: Add an option to yargs
        • Setup a body option for the add command. Under the builder section I copied the title json and changed to body.
        • Configure a description, make it required, and for it to be a string. The title object was already setup for requred and true.
        • Log the body value in the handler function. Just added a second console.log
        • Test your work!

    5. 18. Storing Data with JSON
      • Here I created the playground folder in my node-course folder in DropBox.
      • I created a file titled 1-json.js
      • This example and exercise uses parse and stringify.
      • Use cd .. to go up one directory
      • JSON.stringify(book) This takes our JSON object book and converts to a string. Notice double quotes around keys and values.
      • JSON.parse(bookJSON) This takes our string and converts to a JSON object.
      • The code for this lesson is in the playground folder underneath node-course.

    6. 19. Adding a Note
      • Outline of my Notes project:
        • app5.js is the entry point for this project.
        • notes.js holds the functions specfic to adding, saving, loading notes.
          • require(s)
          • getNotes()
          • addNote()
          • saveNote()
          • loadNotes()
          • module exports
        • notes.json holds my data
      • Download the Resource Files for this lesson. Print out the code I need to enter into my project.
    7. 20. Removing a Note
    8. 21. ES6 Aside: Arrow Functions
    9. 22. Refactoring to Use Arrow Functions
    10. 23. Listing Notes
    11. 24. Reading a Note