This is the seventh day of my participation in the August More text Challenge. For details, see:August is more challenging

Basic concepts of NodeJS

Nodejs was released in 2009 by Dyan Dahl. Nodejs is a platform for running JS on a computer. Before it, JS could only run in a browser.

So you can test it out, we can use the alert message in the browser console, but we can’t recognize it in the computer terminal

Nodejs component

chrome

  • HTML
    • webkit
  • JavaScript
    • V8
  • The middle layer
  • Network card, hard disk, graphics card…

Node

  • JavaScript
    • V8
  • Intermediate layer (LIbuv)
  • Nic, hard disk…

Thus, the structure of Node is very similar to that of a browser, except for the absence of webKit, graphics cards, and other technologies related to UI presentation.

The characteristics of nodejs

Asynchronous I/O

Due to the characteristic of js single thread, if the operation such as reading files is synchronous operation, with the increase of files, this time-consuming task will occupy the single thread for a long time, which will lead to the subsequent tasks cannot be executed.

So the nodeJS authors have built a lot of asynchronous I/O apis at the bottom, reading from files to network requests, and so on. By doing so, NodeJS naturally handles parallel I/O operations, each without waiting for the last operation to finish. For example, read files asynchronously.


const fs = require('fs');
const fileName = 'target.txt';

const errHander = (err) = > {
  console.log(err);
}

const dataHander = (data) = > {
  console.log(data.toString())
}

fs.readFile(fileName, (err, data) = > {
  if(err){
    errHander(err);
  }
  dataHander(data);
})
Copy the code

Compared to other languages such as PHP and Ruby, multithreading blocks IO. Multithreading means that every time there is a new task, one more thread needs to be opened. For example, connecting to a database and looking up data from the database may wait a few seconds, during which time the current thread like PHP and Ruby will be blocked, so you need to open a separate thread.

Nodejs does not block because of the callback function, described below

Event and callback functions

Events are characterized by being lightweight, loosely coupled, and focusing only on the point of the matter.

The callback function is very familiar to the front end students, or the above example for example, the code is written in order and the order of execution does not matter, after the callback function execution is completed, the result will be put into the callback queue, after the single thread execution is completed, put into the thread execution

Single thread

As mentioned many times before, the benefits of single threading are that there is no need to worry about synchronizing state between threads like multithreading, no deadlock, and no performance overhead caused by thread context exchange

Single threading has its weaknesses: it can’t take advantage of multi-core cpus; An error causes the entire application to exit; A computationally heavy task, which occupies the CPU, also causes asynchronous I/O calls to fail.

To address these issues, NodeJS provides apis to create child processes (child_process) to address robustness and the inability to utilize multi-core cpus, as well as to spread a large number of computations across child processes and deliver results through interprocess message communication.

cross-platform

Nodejs can run on both Windows and * Nix based on the compatibility of Libuv, which is the basis of many systems’ cross-platform implementation. Nodejs’s third-party C++ extension module is also cross-platform via libuv.