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 defaultsnpm i <package>
: installnpm un <package>
: uninstallnodemon src/app.js -e js,hbs
: changes refreshed included hbs and js files
process.argv
: Arguments__dirname
: current directory__filename
: current file
Core modules 3rd party modules Your own modules
Define all of the things we can share with other files
modules.export = <value>
chalk
: Colours for terminalnodemon
: See changes instantlynodemon app.js
yargs
: Argument processingrequest
: For HTTP requestsexpress
: Web serverhbs
: View engine forexpress
based onhandlerbars
(based on Mustache templating)reactive-native-cli
: For building react native applications
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)
}
})
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)
array.forEach(function)
array.filter(function)
array.find(function)
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.
setTimeout(() => {
console.log('Non-blocking. This message will display after 2 seconds')
}, 2000)
- JavaScript modules (import/export)
- CommonJS (require/exports)
# Set default alias
nvm alias default v16.14.2
# Use based on .nvmrc
nvm use
# Use specific
nvm use 16
More information here
#!/usr/bin/env node
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)