Node.js module system
In order to allow Node.js files to call each other, Node.js provides a simple module system.
Modules are the basic components of Node.js applications, and files and modules have a one-to-one correspondence. In other words, a A Node.js file is a module, which may be JavaScript code, JSON, or a compiled C/C++ extension.
Creating a module
In Node.js, creating a module is very simple. As follows we create a 'main.js' file with the following code:
var hello = require('./hello'); hello.world();
In the above example , the code require('./hello') introduces the hello.js file in the current directory (./ is the current directory, and the default suffix of node.js is js).
Node.js provides two objects, exports and require, where exports is the interface exposed by the module, and require is used to obtain the interface of a module from the outside, that is, the exports object of the obtained module.
Next we will create the hello.js file, the code is as follows:
exports.world = function() { console.log('Hello World'); }
In the above example, hello.js uses world as the access interface of the module through the exports object, in main.js Load this module through require('./hello'), and then you can access it directly Ask about the member functions of the exports object in hello.js.
Sometimes we just want to encapsulate an object into a module. The format is as follows:
module.exports = function() { // ... }
For example:
//hello.js function Hello() { var name; this.setName = function(thyName) { name = thyName; }; this.sayHello = function() { console.log('Hello ' + name); }; }; module.exports = Hello;
In this way, we can directly obtain the object:
//main.js var Hello = require('./hello'); hello = new Hello(); hello.setName('BYVoid'); 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 output, not the original exports.
Where to put the server-side modules
Perhaps you have noticed that we have used modules in the code. Like this:
var http = require("http"); ... http.createServer(...);
Node.js comes with a module called "http", we request it in our code and assign the return value to a local variable.
This turns our local variable into an object with all the public methods provided by the http module.
The file search strategy in the require method of Node.js is as follows:
Since there are 4 types of modules (native modules and 3 types of file modules) in Node.js, although the require method is extremely simple, However, the internal loading is very complicated, and its loading priorities are also different. As shown in the figure below:
Loading from the file module cache
Although the priorities of native modules and file modules are different, they will not take precedence over loading from the cache of the file module. Already existing module.
Loading from native modules
The priority of native modules is second only to the priority of file module cache. After parsing the file name, the require method first checks whether the module is in the native module list. Take the http module as an example. Although there is a http/http.js/http.node/http.json file in the directory, require("http") will not be loaded from these files, but from the native module.
Native modules also have a cache area and are also loaded from the cache area first. If the cache area has not been loaded, the native module loading method is called for loading and execution.
Loading from file
When the file module cache does not exist and is not a native module, Node.js will parse the parameters passed in the require method and load the actual module from the file system. File, packaging and compilation details during the loading process have been introduced in the previous section. Here we will describe the process of finding file modules in detail. There are also some details worth knowing.
The require method accepts the following parameters:
http, fs, path, etc., native modules.
./mod or ../mod, file module with relative path.
/pathtomodule/mod, file module with absolute path.
mod, file module of non-native module.