Related articles

  • P01: Node.js tutorial from a practical perspective
  • P02: Basic node usage
  • P03: Node built-in module PATH
  • P04: Buffer for nodeAPI
  • P05: Events of node built-in module
  • P06: Node built-in module FS (1)
  • P07: Node built-in module FS (2)
  • P08: Node implements static server ~ create project
  • P09: Node implements static server ~ Hello HTTP
  • P10: Node implements static server ~ static file or folder reading

node

With the continuous development of front-end technology, especially the popularity of front-end engineering in recent years, Node.js has received more and more attention and use. This series of articles will be their own study of node in the summary of experience presented one by one, the level is limited if there is a mistake, please correct it, thank you very much.

The official documentation

  • Node 英 文 版
  • Node of the original document

What is a node. Js?

Node.js is a JavaScript runtime based on the Chrome V8 engine

  • It is a Javascript runtime environment

  • Rely on Chrome V8 for code interpretation

  • event-driven

  • Non-blocking I/O

  • Lightweight and scalable, suitable for real-time data interaction applications

  • Single process, single thread

The advantages of the node. Js

  • For the front end, it helps to unify the experience
  • High concurrency and IO intensive performance

    Difference between I/O intensive and CPU intensive

    • CPU intensive: encryption and decryption, compression and decompression => Operation dense
    • I/O intensive: file operations, network operations, and database operations are intensive

Single thread of Node.js

  • Single threading is for the main process of the system, I/O operations handed over to the underlying system multiple processes to run
  • The single thread of Node.js is not a single thread within a single process and can be used to call multiple processes (one for each single core) to complete the cluster. As long as you need, the machine will not sit idle

Front knowledge

  • Briefly understand blocking I/O and non-blocking I/O
    • If I/O is blocked, wait for the result of the previous step to return
    • Non-blocking I/O: asynchronous => Execute the next step without relying on the result of the previous step
  • Processes and threads
    • process: is a running activity of a program in a computer on a data set. It is the basic unit of resource allocation and scheduling in the system.Analogies to modules
      • The process is generally composed of three parts: program, data set and process control block.
        • Program is used to describe the process to complete the function, is the control process execution instruction set;
        • The data set is the data and workspace required by the program during execution.
        • The program control block contains the description and control information of the process, which is the only sign of the process’s existence
      • Characteristics of a process:
        • Dynamic: process is an execution process of the program, which is temporary and has a life span, dynamic generation and dynamic extinction.
        • Concurrency: Any process can execute concurrently with other processes.
        • Independence: a process is an independent unit of the system for resource allocation and scheduling.
        • Structure: process is composed of program, data and process control block
      • Multi-process: Run multiple processes at the same time => For single-core only one process can be run at the same time. The implementation of multi-process is roughly understood as the rapid switching of multiple processes, similar to frame-by-frame animation
    • thread: The smallest unit of program execution flow. It is the basic unit of processor scheduling and dispatching. It is a relatively independent, callable execution unit within a process that shares the resources of the process with threads belonging to the same process.Analogies to components
      • Multithreading: Using a process to run multiple threads simultaneously
    • The difference between processes and threads
      1. Thread is the smallest unit of program execution, and process is the smallest unit of resources allocated by the operating system;
      2. A process consists of one or more threads, which are different execution paths of code in a process
      3. Processes are independent from each other, but each thread in the same process shares the program’s memory space (including code segments, data sets, heap, etc.) and some process-level resources (such as open files and signals, etc.). Threads in one process are invisible to other processes.
      4. Scheduling and switching: Thread context switching is much faster than process context switching
    • Extension:Why multithreading instead of multiprocessing?
      • Threads are cheap, start faster, exit faster, and have less impact on system resources. Moreover, threads share ownership of most of the core object (File Handle) with each other
      • If multiple processes are used, they are unpredictable and difficult to test

Environment to prepare

  • Open the CMD
$ node -v
vx.x.x
Copy the code
  • If no version number is displayed
    • Download the node
    • All kinds of family barrels in the country have exercised you how to install software
    • It is recommended to learn to use the latest version, production use stable version or follow the company version
    • After the installation is complete, run the command again to obtain the version information, that is, prepare the Node environment

At the beginning of the node experience

// create file demo.js console.log('hello node'// Execute the node demo.js fileCopy the code
  • It Never Rains In Southern California

close