今天,说一个常见的知识点插件。对于不熟悉mongoose的人可能会问mongoose中也有插件?这个别说还真的有。那么,在mongoose中的插件如何使用?mongoose插件的使用它和通常用的JavaScript的插件一样,都是为了实现代码的重用。同mongoose再认识(二)中介绍的方法类似。可以在Schema的实例上添加。首先,介绍一个api schema.add(),这个方法可以实现对Schema的扩充。那么,可以紧接着mongoose再认识(二)中的代码来说,修改它的代码如下:let UserSchema = new mongoose.Schema({ firstname: String, lastname: String})UserSchema.add({ createAt: { type: Date, default: Date.now }, updateAt: { type: Date, default: Date.now }})UserSchema.pre(‘save’, function(next) { let now = Date.now() this.updateAt = now; if (!this.createAt) this.createAt = now;})将createAt和updateAt的代码提取出来,因为在开发中,很多collection都需要它们,同样也可能需要用到它的处理方法。所以,用一个插件将它们封装起来变得很有必要。可参考如下代码:module.exports = function(schema) { schema.add({ createAt: { type: Date, default: Date.now }, updateAt: { type: Date, default: Date.now } }) schema.pre(‘save’, function(next) { let now = Date.now() this.updateAt = now; if (!this.createAt) this.createAt = now; })}文件名为time-plugin.js然后,在使用它的UserSchema定义中引用它。代码如下:let UserSchema = new mongoose.Schema({ firstname: String, lastname: String})let timePlugin = require(’../plugins/time-plugin.js)userSchema.plugin(timePlugin)在cnode-club的源码中定义了一个base_model.js文件,这个文件分别在topic.js 、 user.js等文件中进行了引用。