Daily soul question – on node Process in JS



Process is node The global object of JS can be used without loading it with require()

The process module mainly does two things

  • 1. Obtain process information (resource usage, running environment, running status, three standard input and output streams)
  • 2. Execute process operations (listen for events, schedule tasks, issue warnings, and exit the process)

1. Obtain process information (resource usage, running environment, running status)

1.1 get process information – resource usage: refers to the machine resources consumed by running this process

process. Memoryusage() memory 
  process.cpuUsage() cpu

1.2 getting process information – running environment

process. Cwd() run directory
  process. Version get node environment
  process. Arch get CPU architecture
  process. env. NODE_ Env gets the user environment and distinguishes between development and production
  process. Platform system platform
  process. Uptime() has been running for seconds

1.3 getting process information – running status

process. Argv can get the passed in command line parameters, and the return value is an array
  process. Execargv returns a node specific command line parameter
  process. Execpath absolute path
  process. Mainmodule determines whether it is the main file

1.4 getting process information – standard I / O stream

process. Stdout standard output
  process. Stdin standard input
  process. Stderr standard error output

2. Execute process operations (listen for events, schedule tasks, and issue warnings)

2.1 execute process operation – listen for the event process on()

process. On ('beforeexit ', CB) can execute asynchronous code
  process. On ('exit ', CB) executes the synchronization code
  process. On ('uncaughtexception ', CB) captures exception information (exception information without try catch)

2.2 execute process operation – schedule task process nextTick(fn)

process. Nexttick (FN) takes the callback function as a micro task and puts it into the task queue of the event loop

2.3 execute process operation – warn

  process.emitWarning('Something warning   happened!', {
      code: 'MY_WARNING',
      type: 'XXXX'

2.4 execute process operation – exit process

  process. Exit ([Code]) terminates the current process and returns the given code
  process. Kill (PID, [signal]) ends the process corresponding to a PID and sends a signal