Home >Web Front-end >JS Tutorial >Application of Node.js modules
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
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();
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)!