1. Know the node

  • Node. js is a Chrome V8 engine JavaScript runtime environment
  • Node == JavaScript core (ECMA) + extension functionality (server language features such as file system, modules, packages, OS apis, network communications, etc.)
  • Node has no DOM and BOM operations

2. The relationship between Node.js and JavaScript

  • Node.js is a runnable environment (a stand-alone parsing engine) for JavaScript, a programming language.
  • Node.js, based on chrome’s V8 engine, enhances the JavaScript language to enable server language development (file system, network communication, database add, delete, change, etc.). JavaScript is an interpreted language, composed of ECMA(core)+DOM(W3C)+BOM(browser), as long as there is a JS engine can be run, browser JS mainly operate BOM and DOM.
  • The node with the V8

The reason why node.js single thread processing high concurrency is based on c++ asynchronous framework libuv layer to achieve,libuv is responsible for handling I/O, V8 is responsible for the interpretation and execution of JavaScript code. Libuv and V8 can be combined with a C++ binding layer.

3. Features of Node.js

  • Single Thread Single Thread

In the traditional server model, most use multi-threading to solve the problem of concurrency, and each client to create a thread in the connection, need to consume 2MB memory, that is to say, in theory, an 8GB server can link the number of users at the same time for about 4000. Node.js is single-threaded, uses non-blocking IO, is event-driven, and can theoretically hold 30,000-40,000 user connections on an 8GB server

  • Non-blocking I/0 Non-blocking asynchronous I/O

For I/ OS like disk access and network access, requests tend to be slow so we want the platform to not block the execution of business logic when reading files or sending messages over the network. Node solves this problem with three techniques: events, asynchronous apis, and non-blocking I/O. To Node programmers, non-blocking I/O is a low-level term. This means that your program can make a request for a network resource while doing something else, and then when the network operation is complete, it will run a callback function to process the result of that operation

Asynchronous I/O refers to: thread if there are any disk read and write or network communication in the execution of the request (collectively known as I/O operations), usually it takes a long time, then the operating system will be deprived of the thread CPU control, make its suspended, will work of resources to other threads at the same time, this way of thread scheduling is called congestion. When the I/O operation is complete, the operating system unblocks the thread, regains control of the CPU, and allows it to continue executing. This I/O mode is normal Synchronous I/O or Blocking I/O.

Multithreaded synchronous I/OCopy the code

Asynchronous I/O (Asynchronous I/O) or non-blocking I/O (I/O) does not block for all I/O operations. When a thread encounters an I/O operation, it does not block and wait for the I/O operation to complete or the data to be returned. Instead, it simply sends the I/O request to the operating system and continues to execute the next statement. When the operating system completes an I/O operation, it notifies the thread that performed the I/O operation in the form of an event that the thread processes at a specific time. To handle asynchronous I/O, threads must have event loops that constantly check for unhandled events and process them in turn.

Single-threaded asynchronous I/OCopy the code

In short, asynchronous I/O is the elimination of multithreading overhead. The cost of creating a thread is very expensive for the operating system. It needs to allocate memory for it, schedule it, and perform memory paging when the thread is switched. The CPU’s cache is emptied, and when the thread is switched back, it has to read information from memory again, destroying the locality of the data

  • Event Driven Event Driven

The Node.js single thread is just a JavaScript main thread, and essentially asynchronous operations are done by thread pools in the computer. Node.js can handle high concurrency with a single thread thanks to c++ ‘s asynchronous framework libuv layer, the loop mechanism, and the underlying thread pool. The high performance of Node.js is also due to its asynchronous blocking OF I/O, so that the execution of the main logic is not affected.

Node.js programs start and end in an event loop. All logic is the event callback function, so Node.js is always in the event loop, and the program entry is the callback function of the first event in the event loop. The event callback function may issue I/O requests or emit events directly during execution. After execution, it returns to the event loop, which checks for unprocessed events in the event queue until the program ends.

4. Install and run

  • Download and install from the official website

  • REPL run mode (read-evaluate-print-loop)

  • Run the JS file directly (must be in the current directory)

  • Check the command node -v NPM -v

  • Common DOS commands

    Md creates subdirectories

    CD Changes the current directory

    Dir Displays the disk directory command

    Path Path setting command (search path for device executable files, valid only for files)

    Copy Copies one or more files to a specified disk

    Exit Exits the command line

5. NPM basic instruction set

NPM install packagename The latest version is installed by default if the version number is not specified

NPM install packagename 0.0.1 Installs the specified version of the module

NPM install packagename –save or -s –save, -s means to save the module version information to dependencies, which is the dependencies field of your package.json file

NPM install packagename -g or –global Install the global module.

NPM install packagename –save-dev -d –save-dev -d In the devDependencies field of your package.json file;

NPM uninstall Packagename [options] Uninstalls installed modules. NPM remove, NPM rm, NPM r, NPM UN, NPM unlink these commands have the same functions as NPM uninstall

NPM outdated lists all modules that are outdated

NPM update [-g] For obsolete modules you can update them using the command above

NPM update [package name] Updates the package with the specified name