The module allows node. js files to be called to each other, the module is the basic part of the node. JS application, the file and module is one by one corresponding, in other words, a node. js file is a module, this file may be JS code, JSON or compiled c/s extension.
Create a module
In node. js, it is very simple to create a module.
An example
Create a file named Main.js with the following code:
var hello=require ("./hello"); Hello.world ();
The code require ("./hello") introduces the Hello.js file in the current directory (./is the current directory, and node. js is the default suffix js). node. JS provides exports and require two objects, where exports is the interface that the module exposes, and require is used to obtain an interface from the outside of a module, the exports object of the acquired module.
Create the Hello.js file with the following code:
exports.world=function () { console.log ("Hello moduleWorld") ;
Hello.js through the exports object to the world as the interface of the module, in the Main.js through the Require ("./hello") load the module, and then you can access hello.js objects exports the member functions.
Encapsulates an object into a module in the following format:
module.exports=function () { ...};
For example:
function Hello () { var name; this. setname=function (thyname) { name=thyname; }; this. sayhello=function () { console.log (""+name); };}; Module.exports=hello;
This allows us to use this object in the following code:
var Hello=require ("./hello"); var hello=new hello (); Hello.setname ("wolfy"); Hello.sayhello ();
The only change in the module interface is the use of Module.exports=hello instead of Exports.world=function () {}. When the module is referenced externally, its interface object is the Hello object itself to be exported, not the original exports.
Server-side Modules
In node. js, some commonly used modules, such as HTTP,FS, are built in.
The file lookup strategy for node. JS's require method is as follows:
Since there are 4 classes of modules in node. js (native modules and 3 file modules), although the Require method is extremely simple, internal loading is very complex and its loading priority is different:
Loading from the file module cache
Although the native module has a different priority than the file module, it does not take precedence over loading the existing module from the cache of the file module.
Load from native module
The finite urgency of the native module is second only to the priority of the file module cache, and the Require method first checks whether the module is in the native module list after parsing the file name. In the case of HTTP modules, although there is a Http/http.js/http.node/http.json file in the directory, require ("http") is not loaded from these files, but is loaded from the native module. The native module also has a buffer, which is also preferred to load from the buffer. If the buffer has not been loaded, then the loading mode of the native module is called to load and execute.
Load from File
When the file module cache does not exist and is not a native module, node. js resolves the parameters passed in by the Require method and loads the actual file from the file system.
The Require method receives several parameters for delivery:
- Http,fs,path, etc., native module.
- ./mod or.. /mod, relative path of the file module
- /pathtomodule/mod, the absolute path of the file module.
- MoD, non-native module of the file module.
Learning materials
Http://www.runoob.com/nodejs/nodejs-module-system.html
[node. js] Module