Home  >  Article  >  Web Front-end  >  Node.js tutorial for beginners (2)

Node.js tutorial for beginners (2)

php中世界最好的语言
php中世界最好的语言Original
2018-03-12 17:19:221498browse

This time I will bring you a novice tutorial on Node.js. What are the precautions for novices using Node.js? . The following is a practical case, let’s take a look.

Module Overview

node.js adopts Modularization structure, and defines and uses modules in accordance with commonjs specifications. Modules and files are in one-to-one correspondence, that is, loading a module, It actually loads the corresponding module file. The
require command is used to specify the loading module. The suffix name of the script file can be omitted when loading.

var moyu = require("./myQuery");var moyu1 = require("./myQuery.js");

The parameter of the require method is the name of the module file. It is divided into two situations. The first situation is that the parameter contains the file path, such as the above. The second situation is that the parameter does not contain the file path. At this time, node goes to the module's installation directory to find the existing module. Like the following:

var moyu = require("myQuery");

Sometimes, a module itself is a directory containing multiple files. At this time, node looks for the mainspecified by the attribute in package.json. ModuleEntry file.

But sometimes if there is no package.json, node will try to find index.js, index.node to load.

Once the module is loaded, it will be cached by the system. If it is cleared and loaded a second time, 304 will be returned.

Core module

Node.js tutorial for beginners (2)

The source codes of the above core modules are all in the lib subdirectory of node. In order to improve the running speed, they will be compiled into binaries.

The core module is always loaded first. If you write an HTTP module yourself, require("http") is still the core module.

Custom module

The node module adopts the commonjs specification. As long as it meets this specification, the module can be customized.
The following is the simplest module. Assume that you create a new moyu.js and write the following content:

Node.js tutorial for beginners (2)

The above code is a module, which passes module. exports variable, a method for external output. The usage of this module is as follows:

Node.js tutorial for beginners (2)

The above code loads the module moyu.js through the require command.
Output the external interface of the module to the variable moyu, and then call moyu. At this time, hello moyu will be output when running under the command line. Node.js tutorial for beginners (2)

The module variable is the top-level variable of the entire module file. Its exports attribute is the module's output interface. If a function is directly output (like moyu.js above), then calling the module is calling a function, but the module can also output an object. The following is for moyu.js Rewrite:

var moyu = new Object();var fn = function(a){    console.log(a);}moyu.fn = fn;module.exports = moyu;
上面的代码表示模块输出moyu对象,该对象有一个fn方法。
var moyu = require("./moyu");moyu("hi,moyu");

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

How to express mvvm principle intuitively with code

Execution principle of Node.js code

The above is the detailed content of Node.js tutorial for beginners (2). 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