Skip to content

Instantly share code, notes, and snippets.

@m0un10
Last active October 2, 2022 06:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m0un10/0eaa9a9c392fa528568ec6c6d2ed9192 to your computer and use it in GitHub Desktop.
Save m0un10/0eaa9a9c392fa528568ec6c6d2ed9192 to your computer and use it in GitHub Desktop.

NodeJS

Basics

Useful default objects in different runtimes:

  • Chrome runtime on V8
    • window
    • document
    • fetch() for calling APIs
  • Node runtime on V8
    • global
    • process

Non-blocking example

getUser(1, (user) => {
  console.log(user)
})

Common convention

  • <function>: Non-blocking
  • <function>Sync: Blocking

Commands

  • npm init -y: initialise with defaults
  • npm i <package>: install
  • npm un <package>: uninstall
  • nodemon src/app.js -e js,hbs: changes refreshed included hbs and js files

Useful global imports

  • process.argv: Arguments
  • __dirname: current directory
  • __filename: current file

Modules

Core modules 3rd party modules Your own modules

Define all of the things we can share with other files

modules.export = <value>

Useful libraries

  • chalk: Colours for terminal
  • nodemon: See changes instantly nodemon app.js
  • yargs: Argument processing
  • request: For HTTP requests
  • express: Web server
  • hbs: View engine for express based on handlerbars (based on Mustache templating)
  • reactive-native-cli: For building react native applications

Yargs

Automatically includes --help and --version

Better argument object with yargs.argv

Change version with yargs.version('1.0')

yargs.command({
    command: 'add',
    describe: 'Add a new note',
    builder: {
        title: {
            describe: "Note title",
            demandOption: true,
            type: 'string'
        }
    },
    handler: function (argv) {
      console.log('Adding a new note', argv.title)
    }
})

JSON processing

const book = {
    title: '12 Rules to Life',
    author: 'Jordan Peterson'
}

const bookJSON = JSON.stringify(book)
console.log(bookJSON)

const parsedData = JSON.parse(bookJSON)
console.log(parsedData.author)

Useful Array Functions

array.forEach(function)
array.filter(function)
array.find(function)

Debugging

Add debugger in your code anywhere for a breakpoint. Then, run node inspect <file> Or, go to chrome://inspect in Chrome to get a nice GUI.

Asynchronous

setTimeout(() => {
    console.log('Non-blocking. This message will display after 2 seconds')
}, 2000)

Import styles

  • JavaScript modules (import/export)
  • CommonJS (require/exports)

NVM

# Set default alias
nvm alias default v16.14.2

# Use based on .nvmrc
nvm use

# Use specific
nvm use 16

W/ Typscript

More information here

#!/usr/bin/env node

Sample Applications

The Complete Node.js Developer Course

  • Note Taking (Fundamentals, Debugging)
  • Weather (APIs)
  • Task App (Promises, REST, Mongo, Security, Emails, Uploads, Testing, Sort/Page/Filter)
  • Chat App (Sockets)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment