Node.js的module.exports与exports

本文探讨了Node.js中module.exports与exports的区别及联系,并通过具体示例展示了如何正确使用它们来导出函数和对象。此外,还分享了一个MongoDB连接DAO的设计案例,解释了在构造函数中使用new关键字的重要性。

今天在写MongoDB的DAO的时候,因为一些疏忽导致出现了例如MongoDB is not a functionCannot read property 'connect' of undefined的错误,所以写这篇博客说说module.exportsexports,顺便理一下自己在写DAO的思路

想先看下官网的东西,地址:http://nodejs.cn/api/

exports

先看一下exportsapi

This variable may appear to be global but is not.

A reference to the module.exports that is shorter to type. See the section about the exports shortcut for details on when to use exports and when to use module.exports.

得到的信息就是首先exports不是全局的,它是module.exports的短引用方式,也就是说它最终使用的还是module.exports

module.exports

关于module.exports,可以看到这里似乎告诉我们更多的东西

exports 变量是在模块的文件级别作用域内有效的,它在模块被执行前被赋于 module.exports 的值,它有一个快捷方式,以便 module.exports.f = ... 可以被更简洁地写成 exports.f = ...

看下这个例子:

1.js

function add(num1, num2) {
    console.log(num1 + num2);
}

exports.add = add;

demo.js

var t = require('./1.js');

t.add(3, 4);

node demo.js

7

2.js

function add(num1, num2) {
    console.log(num1 + num2);
}

exports.add = add;

module.exports = {
    sayHello : function() {
        console.log('hello');
    }
}

demo.js

var t = require('./1.js');

t.sayHello();
t.add(3, 4);
hello
demo.js:4
t.add(3, 4);
  ^

TypeError: t.add is not a function
at Object.<anonymous> (demo.js:4:3)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:509:3

刚开始通过exports导出add函数,但是在它的后面使用了module.exports导出了一个sayHello函数,因为exports最终还是借助了module.exports,所以很明显就是被覆盖了

说说我的“DAO”

就先以一个数据库连接举例吧

刚开始的设想是这样:

var mongoClient =  require('mongodb').MongoClient;

function MongoDB(url) {
    if(this.url) {
        return;
    }
    this.url = url;
}

MongoDB.prototype.connect = function(url, callback) {
    if(arguments.length == 2 && typeof url == 'string') {
        this.url = url;
    }
    if(!this.url) {
        console.log("url不符合");
        return;
    }

    mongoClient.connect(this.url, function(err, db) {
        callback(err, db);
    });
}


module.exports = MongoDB;

刚开始的设想是这样的,就是得到一个实例,如下这样:

var MongoDB = require('model.js');

var url = 'mongodb://localhost:27018/learn';
MongoDB(url).connect(function(err, db) {
  console.log("连接");
})

运行就出现了:

Cannot read property ‘connect’ of undefined

然后我去网上也看了一下,自己写了demo:

//1.js
function People(name) {
    this.name = name;
}

People.prototype.getName = function() {
    console.log(this.name);
}

module.exports = People;

//demo.js

var t = require('./1.js');

t('vamous').getName();

执行之后:

C:\*\demo.js:3

t('vamous').getName();
^

TypeError: Cannot read property 'getName' of undefined
at Object.<anonymous> (C:\*\demo.js:3:12)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:509:3

后来发现是自己没有使用new关键字,在使用之后,就可以得到想要的结果了,这次也让我深刻的体会到了有new和没有的差别,没有的时候,真的只是去执行一个函数,而有new的时候,才真正会产生实例

所以我前面的DAO也可以这样写:

var url = 'mongodb://localhost:27018/learn';
var MongoDB = require('./model/model.js');
var db = new MongoDB(url);

....

在写这篇博客的时候,感觉自己的思路更加清晰,希望你也有所收获

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值