Home  >  Article  >  Web Front-end  >  What are the features of node.js

What are the features of node.js

青灯夜游
青灯夜游Original
2022-07-18 17:56:434157browse

nodejs has three characteristics: 1. Server side; Nodejs runs on the server side and provides environment services for the running environment of Javascript. 2. Non-blocking asynchronous; Nodejs adopts a non-blocking I/O mechanism, which will not cause any blocking when performing I/O operations. 3. Event-driven; when a new request comes in, the request will be pushed into an event queue, and then a loop is used to detect event status changes in the queue. If an event with a status change is detected, then execute The processing code corresponding to this event.

What are the features of node.js

The operating environment of this tutorial: Windows 7 system, nodejs version 16, DELL G3 computer.

What is Node.js?

Node.js is an open source and cross-platform JavaScript runtime environment, or in other words, a platform.

Run the V8 JavaScript engine (the core of Google Chrome) outside the browser, using technologies such as event-driven, non-blocking and asynchronous input and output models to improve performance.

It can be understood that Node.js is a server-side, non-blocking I/O, event-driven JavaScript running environment.

As described above, it is not difficult to conclude that Node.js has three characteristics: server-side, non-blocking I/O, and event-driven. Next, we analyze the three features:

  • ① Server side: Literally, Node.js runs on the server side and provides environmental services for the running environment of Javascript.

  • ② Non-blocking asynchronous: Node.js adopts a non-blocking I/O mechanism, which will not cause any blocking when doing I/O operations. When completed, Notification in the form of time to perform the operation.

    For example, after the code that accesses the database is executed, the code behind it will be executed immediately, and the processing code for the database return result is placed in the callback function, thereby improving the execution efficiency of the program.

  • ③ Event-driven: Event-driven means that when a new request comes in, the request will be pushed into an event queue, and then a loop is used to detect the event status in the queue. Change, if an event with a state change is detected, then the processing code corresponding to the event will be executed, usually a callback function

    For example, when reading a file, after the file is read, the corresponding state will be triggered , and then process it through the corresponding callback function

Knowledge expansion:

node.js is a single-threaded service (JS originally does not Both the browser side and the server side are single-threaded). At the same time, it has the world's largest open source library ecosystem: npm.

What are the advantages and disadvantages of Node.js?

##> Advantages

  • Better performance in handling high-concurrency scenarios

  • Suitable for I/O-intensive applications, the value is that when the application is running at the limit, the CPU usage is still relatively low, and most of the time is doing I/O hard disk memory read and write operations

> Disadvantages

Because Node.js is single-threaded, the disadvantages are:

  • Not suitable for CPU-intensive applications

  • Only supports single-core CPU and cannot fully utilize the CPU

  • The reliability is low. Once a certain link of the code crashes, the entire system will collapse.

What are the features of node.js

What can Node.js do?

    Provide data to the browser for display
  • Save the data submitted by the user
  • Data statistics and analysis

The module system of Node.js

There is no concept of global scope in

Node.js; In
Node.js, multiple JavaScript script files can only be loaded and executed through the require method;
require Loading can only execute the code in it. Since the files are in the module scope, there will be no pollution problem;

  • The module is completely closed

  • The outside cannot access the inside

  • The inside cannot access the outside

Although the module scope can bring some The advantage is that multiple files can be loaded and executed, and the problem of variable naming conflict contamination can be completely avoided.

But in some cases, modules need to communicate. In each module, an object is provided: '
exports', which is an empty object by default. During use, members that need to be used for external access are manually mounted to the '
exports' interface object. Who will then '
require' this module? , who can get the exports interface object inside the module.

Core module

Core module is a named module provided by Node. They all have their own special name identification, for example:

  • fs:File operation module

  • http: Network service building module

  • os: Operating system information module

  • path: Path processing module

All core modules must be used manually before usingrequire method to load before it can be used, for example: 'var fs = require('fs')'

Node.js application scenario

With the help of the characteristics and disadvantages of Nodejs, its application scenarios are classified as follows:

  • Good at I/O, not good at calculation. Because Nodejs is a single thread, if there are too many calculations (synchronization), this thread will be blocked; No very complicated processing is required;

  • cooperates with websocket to develop long-connected

    real-time interactive applications
  • , that is: real-time communication;
  • The specific scenarios can be expressed as follows: ① User form collection system, background management system, real-time interaction system, examination system, networking software, high-concurrency web Application;

  • ② Multiplayer online games based on
web, canvas

;

③ Multiplayer real-time chat client and chat based on

web

Room, graphic and text live broadcast; ④ Single-page browser application;

⑤ Operate database and provide json-based API for front-end and mobile terminals; In fact,

Node.js

can realize almost all applications, but what needs to be considered is whether the current scenario is suitable for using Node.js, whether using Node.js is the "optimal solution", and performance issues need to be considered.

For more node-related knowledge, please visit:

nodejs tutorial

!

The above is the detailed content of What are the features of node.js. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn