node's module.exports and exports

One, module.exports and exports

The module system in Node follows the CommonJS specification.
Then the question comes again, what is the CommonJS specification?
Since js used to be confusing, each code was written, there was no concept of a module, and this specification is actually a definition of the module.

The modules defined by CommonJS are divided into: module identification (module), module definition (exports), module reference (require)

First explain exports and module.
When a node executes a file, exports and module objects will be generated in the file,
and the module has an exports attribute. The relationship between them is shown in the figure below, and they all point to the same {} memory area.

exports = module.exports = {
    
    };

Usually, it is recommended to use module.exports to export, and then use require to import.

Second, the basic use case

1,exports和require

When using exports

let myFunc1 = function() {
    
     ... };
let myFunc2 = function() {
    
     ... };
exports.myFunc1 = myFunc1;
exports.myFunc2 = myFunc2;
const m = require('./mymodule');
m.myFunc1();

That is, the introduced m object contains all the exports of the file.

2,module.exports和require

const a=function a(ctx) {
    
    
    ctx.body={
    
    
        "message":"hello from a"
    }
}
module.exports=a
const Router=require('koa-router')
const a= require('../api/ayewu')

const router =new Router()
router.get('/a',a)

module.exports=router

At this time a is the function defined above

Three, understand again

It is not difficult to see that module is a variable declared by node.js itself for each .js file. At the same time, this variable has two parameters, one is the index id, the other is an object variable named exports, and exports are used to store user exports. Variable or attribute. What we have to do when exporting externally is to add the variables or attributes that need to be exported to the exports object under the module variable in the form of key-value key-value pairs. If you export one by one like the following:

function hello() {
    
    
    console.log('Hello, world!');
}

function greet(name) {
    
    
    console.log('Hello, ' + name + '!');
}

module.exports.hello = hello;
module.exports.greet = greet;

After the require import is named test. test.hello is the function hello, and test.greet is the function greet.
So it can be introduced and used in main.js:
Insert picture description here

Module is a variable declared by node.js itself for each .js file, and there is a parameter exports in it. This object stores all the content exported by the file.
So, you can write like this, which is more convenient and faster

function hello() {
    
    
    console.log('Hello, world!');
}

function greet(name) {
    
    
    console.log('Hello, ' + name + '!');
}
let varible="这是一个变量"
module.exports={
    
    
    hello,
    greet,
    varible
}
//是这个的简写
// module.exports={
    
    
//     hello:hello,
//     greet:greet,
//     varible:varible
// }

Guess you like

Origin blog.csdn.net/weixin_42349568/article/details/114903459