node - GradedJestRisk/js-training GitHub Wiki
Handful:
- asynchronous, non/blocking IO implemented in a single threaded
- "the main event loop is single-threaded but most of the I/O works run on separate threads"
- Check Deno to seeNode.js vulnerabilities
More here List:
Node Version Manager (NVM) allow easy version switching for:
- node;
- node package manager (npm).
Install for:
- bash
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.2/install.sh | bash
- activate auto-completion
npm completion >> ~/.zshrc
- zsh
-
https://github.com/lukechilds/zsh-nvm
, section "As an Oh My ZSH! custom plugin"
-
- upgrade:
nvm upgrade
Latest:
- check who is the latest:
nvm ls-remote | tail
- OR
nvm ls
- install latest:
nvm install node
- activate:
nvm use node
- check installed (green arrow) :
nvm ls
- install latest:
nvm install 14.4.0
- activate:
nvm use 14.4.0
- check installed (green arrow) :
nvm ls
Expect you to create a config file (.nvmrc) per repository, as nvm does not read "engine key" from package.json as for now
Use the current version: node -v > .nvmrc
Or create the .nvmrc file manually
Steps:
- restart your terminal
- list available versions and choose an old one:
nvm ls
- switch to this version:
nvm use <VERSION_NUMBER>
- enter the folder with .nvmrc file, you get the following message
Found '.nvmrc' with version <13.8.0> Now using node v13.8.0 (npm v6.13.6)
Create helloWorld.js
#! /usr/bin/env node let people = 'node'; console.log("\"Hello, world ! \" says", people);
Execute node helloWorld.js
, uou'll get "Hello, world ! " says node
List:
- source in src
- test:
- unit in test
- BDD in spec
- resources in public
- images
- css
- js for client-side, not compiled
all headers are made lowercase
From here
If a standalone script, if you want to have a single entry-point
const mainFunction = function(people){ console.log('Hello, ' + people + ' !'); } if (require.main === module) { mainFunction('world'); }
Each file is a module. As each module hide its content by default (encapsulation), you should explicitly defined which element should be exposed. This is done through module's export
property.
module.exports is a hook on which you can add any properties:
- function (standard & class)
- values (const / let)
- object
prototype.js
const makeAgedBrie = function (agedBrieParams) {(..)}; // export module.exports.Item = Item; module.exports.makeAgedBrie = makeAgedBrie; OR (shorthand) module.exports = { Item, makeAgedBrie }
If using exports
instead of module.exports
, make sure you know what you do
Import is intended to expose another module interface into the current module, so you can call it.
prototype.test.js
// import const {Item, makeAgedBrie} = require('./prototype'); // use const standardItem = new Item(standardItemParams) ;
require
:
- is synchronous;
- use cache : when two module use same one, its state is shared see here
- OS is case-sensitive (eg. Linux)
- use npm 3 or higher (since 2016)
- use same module & dependencies version
require
also execute the code from the exported module.
Usually, the exported module does nothing by itself, but defining functions and exposing them in module.exports
. However, the following is allowed:
- the required module execute code (if not already imported in another module, because of cache)
- the requiring module discard the returned reference
Allow SLL connection with self-signed certificate
NODE_TLS_REJECT_UNAUTHORIZED='0' node
List:
To launch an async function on node REPL, you'll need an async IIFE
const messageBus = require('./message-bus'); ( async() => {messageBus.sendEmailChange({id: 0, newEmail: '[email protected]'}) })();
One-liner:
( async() => {require('./message-bus').sendEmailChange({id: 0, newEmail: '[email protected]'}) })();
To get additional informations, use `NODE_DEBUG` environment variable:
-
NODE_DEBUG=* node
: verbose -
NODE_DEBUG=net node
: HTTP call -
NODE_DEBUG=module node
: module resolution (require trace)
Node should exit after executing all functions If not, you may want to use process.exit(EXIT_CODE) to force exit
Better use this package to track what prevents node to exit