Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the built-in module in Node.js

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge points of this article "how to use the built-in module in Node.js", so the editor summarizes the following, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use the built-in module in Node.js" article.

Built in module

Some chestnuts.

Node.js built-in module operation mechanism

Through Node.js source code analysis

The application layer code calls the Node.js module

The Node.js module calls the underlying C++ module through internalBinding

The built-in module of Node is stored in the lib folder

The built-in module calls methods at the internalBinding V8 level

InternalBinding is in the C++ code in the src directory

The C++ code defines some underlying methods, which are exported through the interface of V8 for call at the Node level.

Finally, the Node layer returns to the application layer

EventEmitter (Observer Mode)

In some cases, the data is not called through the Node.js source code, but directly notified to the Node.js code by the operating system to do something, such as EventEmitter

Chestnut process.stdin.on ("data", (e) = > {const playerAction = e.toString () .trim ();})

The principle of on events is implemented using Class: EventEmitter

EventEmitter can pass some changes that occur at the bottom, such as receiving a mouse event, to the application layer, so that developers can do the corresponding operations.

Event listener application scenario

Using observer mode to solve the problem of communication between multiple module objects

/ / index.jsconst EventEmitter = require ("events"). EventEmitter;class GeekTime extends EventEmitter {constructor () {super (); setInterval () = > {this.emit ("newLesson", {price: Math.random () * 100});}, 3000);} const geekTime = new GeekTime (); module.exports = geekTime;// buy.jsconst geekTime = require (". / index.js") GeekTime.addListener ("newLesson", (res) = > {console.log ("New lessons!", res.price); if (res.price < 80) {console.log ("Price is less than 80, buy!") ;})

Node.js Chestnut: EventEmitter

Browser Chestnut-addEventListener-removeEventListener

The distinction between Observer Mode and publish-subscribe Model

In the publish-subscribe model, the registration and trigger of events take place on a third-party platform independent of both parties. JS implementation-callback function observer mode: the publisher touches the subscriber directly. JS implementation-throwing an event

The above is about the content of this article on "how to use the built-in modules in Node.js". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report