nodejs加载模块心得,mongoose的继承,schematype的mixd介绍

1. require("xxx")可以是原生模块, 也可以是根目录“/node_modules”下的某个模块

2. 多个模块的package.json使用同一个相同模块的时候,将改公用模块写到根目录下的dependencies中,并且注意版本号,下载大家都可以公用的版本号。

若两个调用方调用的版本不一致很容易重复加载相同的模块

例如:

connect-mongo的package.json的dependencies需要"mongodb": "1.3.x",

mongoose的package.json的dependencies需要"mongodb": "1.3.19"

那么我们应该在根目录的dependencies中写:"mongodb": "1.3.19",

这样两者都能公用,也就不会分别下载一个不同版本的mongodb了。

3. 关于mongoose的继承问题:

http://nodejs.org/docs/latest/api/util.html

可以使用node的util.inherits方法来解决

mongoose和util.inherits联用:

https://github.com/LearnBoost/mongoose/pull/1647

4. mixed介绍:

http://mongoosejs.com/docs/schematypes.html#mixed

调用save方法的时候,mixed类型将失去更改感应的功能

因此必须在使用save方法前调用.markModified方法才能通知mongoose去保存更改

Since it is a schema-less type, you can change the value to anything else you like, but Mongoose loses the ability to auto detect and save those changes. To "tell" Mongoose that the value of a Mixed type has changed, call the .markModified(path) method of the document passing the path to the Mixed type you just changed.

指定array类型相当于指定了mixed类型的数组