What is Node.js not?

  • Node.js is not a Web backend framework and cannot be compared to Spring and Flask
  • Node.js is not a programming language and cannot be compared to Python and PHP

What is Node.js?Wiki

  • Node.js is a platform

    • It combines multiple technologies
    • It allows JavaScript to also call system interfaces to develop back-end applications
  • What technologies are used in Node.js

    V8 engine, Libuv, C/C++ implemented C-ARES (DNS parsing), HTTP-Parser (HTTP parsing), OpenSSL (HTTPS), Zlib (compression) and other libraries.

    If you are interested in the source code implementation, it is recommended to see:

    Node.js V0.10 (small amount of code, easy to read)

    Node.js source code analysis

Node.js technology architecture

The Node.js technology architecture is shown here, and here’s a quick look at what they do

bindings

If C/C++ implements a XXX library, it is so useful that JS cannot call it directly. Node.js encapsulates it in C++ to make it meet certain requirements. The wrapped file is called xxx_binding.cpp, and then compiled into a.node file using Node.js (not the only way, there are other ways), and then JS can require this file directly. Bindings are the bridge between JS and C/C++ libraries, and Node.js provides a lot of bindings, so add an S

libuv

A cross-platform asynchronous I/O library that automatically selects the appropriate solution based on the system

Used for TCP/UDP/DNS/file asynchronous operations

What is I/O?

All input and output belong to I/O, such as file reading and writing, network access, and file sending and receiving

V8

function
  • Change JS source code to native code execution
  • Maintain the call stack to ensure the order in which JS functions are executed
  • Memory management
  • Garbage collection, reuse of unused memory
  • Standard library to implement JS
Pay attention to
  • V8 does not provide a DOM API
  • V8 executes JS single-threaded
  • You can start two threads to execute JS separately
  • V8 itself is multithreaded
  • V8 has its own event loop, but Node.js makes one based on Libuv

A brief description of the Event Loop:

An Event

Timer expiration, file readable, file read error, socket content, socket down, these are all events

Loop (Loop)

Events are prioritized, for example:

setTimeout(f1,100)
fs.readFile('/1.txt',f2)
server.on('close',f3)
Copy the code

If all three of these events occur at the same time, there must be an artificial priority rule, and Node.js will poll the events according to this rule, such as 1->2->3->1->2->3

Event Loop

Recommended reading: Event Loop, Timer, nextTick- Fang Yinghang

The operating system triggers events, JS processes events, and Event Loop is the management of Event processing sequence

Execution order

Key points:

  • Timer Check timer
  • Poll Checks system events
  • Check setImmediate callback
  • Other stages are rarely used

Most of the time, Node.js is parked in the poll polling phase. Most events, such as files and network requests, are also processed in the poll phase.

setTimeout(f1,0) 
setImmediate(f2)
// The top code is setImmediate most of the time
// Because most of the time Node.js stays in the poll phase
// To get to the timer phase, you need to go through the check phase
// The setImmediate callback will be executed first
Copy the code

conclusion

  • Asynchronous I/O operations with Libuv
  • Use event loop to manage the order of event processing
  • DNS/HTTP with C/C++ library efficiently…
  • Bindings are used to enable JS to communicate with C/C++
  • Run JS with V8
  • Simplify JS code with the Node.js standard library
  • This is the Node. Js

Node.js API

Node.js API is an application interface opened by Node.js to invoke the above functions. You can view the document when you need to use it

Document address: English and Chinese