session-2 (nodejs.dev documentations)

اشکان نصیرزاده

اشکان نصیرزاده

How to exit from a Node.js program

How to read environment variables from Node.js Where to host a Node.js app ( ngrok, Glitch)
Node.js, accept arguments from the command line Output to the command line using Node.js Accept input from the command line in Node.js Expose functionality from a Node.js file using exports An introduction to the npm package manager The package.json guide
The package-lock.json file
Find the installed version of an npm package Install an older version of an npm package

How to exit from a Node.js program

process.exit(1)
-------------------------------------
process.exitCode = 1
-------------------------------------
process.on('SIGTERM', () => {
  server.close(() => {
    console.log('Process terminated')
  })
})
-------------------------------------
process.kill(process.pid, 'SIGTERM')

How to read environment variables from Node.js

process.env.NODE_ENV // "development"

Where to host a Node.js app ( ngrok, Glitch)

Node.js, accept arguments from the command line

node app.js joe
---------------------------------
node app.js name=joe
----------------------------------
process.argv.forEach((val, index) => {
  console.log(`${index}: ${val}`)
})
------------------------------------
const args = process.argv.slice(2)

Output to the command line using Node.js

console.log('My %s has %d years', 'cat', 2)
console.log('\x1b[33m%s\x1b[0m', 'hi!')

Accept input from the command line in Node.js

const readline = require('readline').createInterface({
  input: process.stdin,
  output: process.stdout
})

readline.question(`What's your name?`, name => {
  console.log(`Hi ${name}!`)
  readline.close()
})

Expose functionality from a Node.js file using exports

const library = require('./library')
------------------------------------------------
const car = {
  brand: 'Ford',
  model: 'Fiesta'
}

module.exports = car

//..in the other file

const car = require('./car')
-------------------------------------------------
const car = {
  brand: 'Ford',
  model: 'Fiesta'
}

exports.car = car

exports.car = {
  brand: 'Ford',
  model: 'Fiesta'
}
--------------------------------------------------
const car = require('./items').car

An introduction to the npm package manager

npm install <package-name>
npm update
npm update <package-name>
npm run <task-name>
npm install -g lodash
# As of npm 5.0.0, installed modules are added as a dependency by default, so the --save option is no longer needed

The package.json guide
The package-lock.json file

Find the installed version of an npm package

npm list

Install an older version of an npm package

npm install <package>@<version>