Home  >  Article  >  Web Front-end  >  Application of Node.js modules

Application of Node.js modules

黄舟
黄舟Original
2017-01-17 15:52:241163browse

Node.js module

Create module

Define module: module.exports = {}

Use module: require(module name)

require searches for modules: File module cache area–> Native module–> File loading–> Cache file module

Application of Node.js modules

Case:

hello.js —Define module

[code]function Hello () {
    var name;
    this.setName = function (aName) {
        name = aName;
    };
    this.getName = function () {
        console.log('Hi, ' + name);
    }
}
module.exports = Hello;

getHello.js—Introduce module

[code]var Hello = require('./hello.js');
var hello = new Hello();
hello.setName('张三');
hello.getName();

Application of Node.js modules

The above is the content of the application of Node.js module. For more related content, please pay attention to PHP Chinese website (www.php.cn)!


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
Previous article:Node.js Stream(stream)Next article:Node.js Stream(stream)