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

The use and Modularization of Node.js

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

Share

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

This article introduces the relevant knowledge of "Node.js usage and modular method". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

What is Node.js ?

Node.js is a JavaScript runtime based on Chrome V8 engine. Similar to JRE in Java, CLR in. Net. Before Node.js was born, JavaScript, as a Web front-end scripting language, has always occupied the king position of front-end development. The reason why browsers can parse JavaScript is based on browser engines. Later, someone [Ryan Dahl] suddenly had a whim, stripped the V8 engine of Google browser from the browser, and perfected it, so that programs written in JavaScript can also be separated from the browser and run as a server, so Node.js came into being. This news made many JavaScript enthusiasts see the dawn of hope, so they joined the open source ecosystem of Node.js. At present, various frameworks and component libraries based on Node.js emerge one after another, which is quite popular with everyone. Node.js also presents a thriving scene.

Node.js Download

Node.js uses an event-driven, non-blocking I/O model that is lightweight and efficient. Node.js package manager NPM is currently the largest open source library ecosystem in the world. Node.js can be downloaded from Node.js Chinese official website [http://nodejs.cn/][latest version: v16.14.0], as shown below:

Node.js not only supports Windows, but also Linux, MacOS, cross-platform support for various environments. Under Windows, download the 64-bit installation package directly, as follows:

Difference between Node.js and JavaScript

At the beginning of learning, it is often unclear the difference between Node.js and JavaScript. Through learning and sorting out, the difference between the two is as follows:

Node.js is an environment based on Chrome V8 engine that can run JavaScript programs, and the JavaScript language has been enhanced [files, networks, operating systems, databases, etc.], so that JavaScript language has server-side program development capabilities.

JavaScript is a programming language that runs as long as there is a JavaScript engine, and each browser has its own browser engine. And if JavaScript runs with the browser, the browser adds interface methods for BOM and DOM operations to JavaScript.

Node.js Installation and Verification

Node.js installation is relatively simple, the default can be used, when the installation is successful, in the command line window through node -v can view the version number, as follows:

Note: Because Node.js is added to the environment variable by default during installation, you can use the node.js command directly.

Node.js How to use

Node.js can be used in two ways:

REPL mode, directly on the command line, executing JavaScript statements.

File mode, i.e. business logic is written in JS file and executed by node file name.

1. REPL mode

REPL [Read-Evaluate-Print-Loop] mode, i.e. input-evaluation-output-loop, i.e. interactive command-line parser mode. Start REPL mode, enter node on the command line to enter the node.js environment, and then execute JavaScript statements, as follows:

Note: The REPL pattern applies only to simple statements and cannot be applied to complex business logic. To exit REPL mode, press Ctrl+C twice.

2. file mode

First create the js file, as follows:

var username="Alan.hsiang";console.log("I am"+username);

First switch the directory to the directory where the js file is located, and then on the command line, execute it by [node file name], as follows:

Node.js modularity

Node.js uses the CommonJS modular specification, so programs developed based on Node.js also need to follow the CommonJS modular specification.

1. What is a module?

In Node.js, all functionality exists as modules. Every js file with a specific function is a module; all user-written code is automatically encapsulated in modules. There may be some dependencies between modules, and these dependencies can be well integrated using modules.

2. module classification

In Node.js, modules fall into two categories:

System modules, modules provided by the system, modules that can be used directly.

Custom modules, user-written modules with specific functions.

3. Create custom modules

Create a js file with the following code:

var username="Little Six Childs"; function sayHi(){ console.log("hi, xiaoliu zi"); } exports.username=username; exports.sayHi=sayHi;

Note: in javascript, objects are exposed to callers through exports. Reference other modules by requiring.

4. Call custom modules

Call custom modules by requiring ("./ File name.js") implementation, as follows:

var obj =require("./ demo01-1.js"); console.log(obj); console.log(obj.username); console.log(obj.sayHi());

Note: The require function returns an object through which to invoke the properties and functions exposed by the custom module.

5. module test

Executed in Node.js, the output looks like this

6. main module

In Node.js, the entry to a module is called the main module, which is responsible for coordinating the other modules composed of the scheduler to complete the specified function. A project can have only one main module, which is declared by default through the main attribute in package.json, usually main.js or index.js. Similar to the Main method of Progrom.cs in. Net or the home page of a website.

7. modules

In Node.js, all user-written code is contained in modules, which are files (functions). This can be seen through the arguments built-in to the function. As follows:

console.log(arguments);

Arguments is a built-in object with five child objects. The specific output results are as follows:

You can view the function body itself through arguments.calle. As follows:

console.log(arguments.callee.toString());

Node.js passes five parameters when encapsulating the module, as follows:

exports: Expose objects by exposing the data in the module to where it is imported.

require: Introduce module function, used to introduce another module in a module, and assign the exposed data of the submodule to the variable.

module: Module object containing all information about the current module.

__filename: filename of the current module

__dirname: Directory path of current module

The output is as follows:

Note: As you can see from the above, the reason why you can use require, exports in your program is because Node.js passes this parameter by default when encapsulating objects.

"Node.js usage and modular approach" content introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

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