In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article focuses on "the modularization mechanism of Node.js and what is the Buffer object". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn "the modularization mechanism of Node.js and what is the Buffer object"!
First, the modularization mechanism of Node.js
Node applications are made up of modules. Node follows the module specification of CommonJS to isolate the scope of each module and make each module execute in its own namespace.
1. What is the CommonJS module specification
CommonJS is a set of code specifications designed to build an ecosystem of JavaScript outside the browser (server-side, desktop-side). Through this specification, JavaScript has the ability to develop complex applications and cross platforms.
2. The standardized content of CommonJS module
(1) Export module: moudle.exports export module
(2) Import module: require ('module name')
/ / create a demo.jsmodule.exports.name = 'yunxi'module.exports.age = 78module.exports.getName = function () {console.log ("name:", this.name);} module.exports.getAge = function () {console.log ("Age:", this.age) } / / create a text.js for testing const person = require ('. / demo') person.getName () person.getAge () / output: name: yunxi// Age: 78 person.getAge / create text.js for testing const person = require ('. / demo') person.getName () person.getAge () / output: name: yunxi// Age: 78 person.getAge / create text.js Output test const Person = require ('. / demo') const p1 = new Person ('Xiao Wang', 39) const p2 = new Person ('Xiao Zhao', 29) p1.getName () p1.getAge () console.log ('-') P2.getName () p2.getAge () / / name: Xiao Wang / / Age: 39max / / name: Xiaozhao / / Age: 293.Each exported module has a moudle object that contains the following attributes:
(1) moudle.exports: indicates the output interface of the current module. Other modules refer to variables exported by moudle.exports.
(2) exports variable: point to moudle.exports. For convenience of operation, it cannot directly point to a value.
4. Import the module using require
Import and execute a JavaScript file that returns an exports object. If the corresponding object is not found, an error will be reported.
(1) if the module output is a function, the output interface of the function cannot be exported with exports variables, but must be exported by user moudle.exports.
(2) loading rules:
A. used to load js files with a default file extension of .js
B. Find the corresponding js file under different paths according to the different formats of the parameters.
'. / (.. /)': indicates that the loading path is relative
'/': indicates that the load path is an absolute path
Neither'. / (.. /) 'nor' /': indicates that the loaded mode is the core module of node, in the node_modules of the node installation path
(3) the internal processing flow of require
Require-- > module.exports-- > moudle._load
2. Buffer object
Is a global object in Node that can be used directly without the need for require import. This object provides an interface for processing binary data in Node
1. It is used to make up for the deficiency of JavaScript in binary data processing. 2. Buffer is a constructor
The Buffer object is a piece of memory allocated by the V8 engine, similar to an array. Each unit is a byte, storing data between 0255s.
3. Basic operations of Buffer:
(1) create: let bytes = new Buffer (size)
(2) slice: bytes.slice (start,end)
(3) copy: bytes.copy (destination buffer, start position of target buffer, start position of source buffer, end position of source buffer)
(4) Interchange between Buffer and string: supported encoding formats (ASCII code, utf8)
(5) the difference between Buffer and binary array:
A, binary array:
Int8Array:-128~127
Uint8Array:0~255
Int16Array:-32768~32767
UInt16Array:0~65535
4. Buffer class:
The Buffer class is based on Uint8Array, so it is an array of integers with a value of 0,255.
At this point, I believe you have a deeper understanding of "what is the modularization mechanism of Node.js and Buffer objects". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.