Module.exports is used if the Module is a specific type. Exports is used if the module is a typical “instantiated object”.

exports.name = function() { 
console.log('My name is Lemmy Kilmister'); 
};
Copy the code

In another file the reference code is as follows:

var rocker = require('./rocker.js'); 
rocker.name(); 
Copy the code

Module.exports is the real interface, exports is just an auxiliary tool. Module.exports is returned to the call instead of exports

All exports collected properties and methods are assigned to module. exports. Module.exports does not have any properties or methods. If Module.exports already has properties and methods, the information collected by exports will be ignored.

Modify rocker.js as follows:

module.exports = 'ROCK IT! '; exports.name = function() { console.log('My name is Lemmy Kilmister'); };Copy the code

The rocker.js code is quoted again as follows:

var rocker = require('./rocker.js'); 
rocker.name(); // TypeError: Object ROCK IT! has no method 'name'
Copy the code

Error found: object “ROCK IT! No name method

The Rocker module ignores the name method collected by exports and returns a string “ROCK IT!” . Thus, your module does not have to return “instantiated objects”. Your module can be any valid javascript object — Boolean, number, date, JSON, String, function, array, etc.

Your module can be anything you set to it. If you do not explicitly set any properties and methods for module. exports, then your Module is an exports property to module. exports.

In the following example, your module is a class with the following code:

module.exports = function(name, age) { 
this.name = name; 
this.age = age; 
this.about = function() { 
console.log(this.name +' is '+ this.age +' years old'); 
}; 
}; 
Copy the code

It can be applied as follows:

var Rocker = require('./rocker.js'); 
var r = new Rocker('Ozzy', 62); 
r.about(); // Ozzy is 62 years old 
Copy the code

In the following example, your module is an array with the following code:

module.exports = ['Lemmy Kilmister', 'Ozzy Osbourne', 'Ronnie James Dio', 'Steven Tyler', 'Mick Jagger'];
Copy the code

It can be applied as follows:

var rocker = require('./rocker.js'); 
console.log('Rockin in heaven: ' + rocker[2]); //Rockin in heaven: Ronnie James Dio 
Copy the code

Adding a property to module. exports is similar to adding a property to exports. Such as:

module.exports.name = function() { 
console.log('My name is Lemmy Kilmister'); 
};
Copy the code

Similarly, exports looks like this, with the following code:

exports.name = function() { 
console.log('My name is Lemmy Kilmister'); 
}; 
Copy the code

Note that these two outcomes are not the same. Module. exports is a real interface, exports is just an accessory. Exports are recommended unless you want to change from an “instantiated object” to a type.

Exports: Exports: Module. Exports: Exports: Module.

  1. exportsIs pointing tomodule.exportsA reference to the
  2. module.exportsThe initial value is an empty object{}, soexportsThe initial value is also{}
  3. require()Returns themodule.exportsRather thanexports

Module. exports is an object that can have properties and methods that refer to direct objects. Method can. Such as:

var mysql = require('mysql'); var config = require('./dbConfig'); var pool = mysql.createPool(config.dbSetting); var db = {}; db.query = function (sql, callback) { pool.getConnection(function (err, conn) { if (err) { callback(err, null, null); return; } else { conn.query(sql, function (error, results, fields) { for (var i = 0; i < results.length; i++) { console.info("%d\t%s\t%s", results[i].id, results[i].name, results[i].pwd); } callback(error, results, fields); }); // Release connection conn.release() after callback; }}); }; db.query_param = function (sql, param, callback) { pool.getConnection(function (err, conn) { if (err) { callback(err, null, null); return; } else { conn.query(sql, param, function (error, results, fields) { for (var i = 0; i < results.length; i++) { console.info("%d\t%s\t%s", results[i].id, results[i].name, results[i].pwd); } callback(error, results, fields); }); // Release connection conn.release() after callback; }}); }; Module. exports = db; // exports as an object.Copy the code

Exports, on the other hand, prefers method definition. Such as:

/** * Created by exports. Tqdy = function (req, res) {res.render('echart/tqdy', {title: 'table voltage curve '}); }; Exports. TQGL = function (req, res) {res.render('echart/ TQGL ', {title: 'echart/ TQGL '}); }; // exports. FZL = function (req, res) {res.render('echart/ FZL ', {title: 'echart/ FZL '}); }; Exports. Bddy = function (req, res) {res.render('echart/bddy', {title: 'echart/bddy'}); // exports. }; // exports. Pddy = function (req, res) {res.render('echart/pddy', {title: 'echart/pddy'}); // exports. };Copy the code