<p>先配置package.json里面依赖npminstall然后引用下mongoose。然后引用mongoose。写存储代码varcrypto=require('crypto');</p><p>varmongoose=require('mongoose');</p><p>mongoose.connect('mongodb://localhost/blog');varuserSchema=newmongoose.Schema({</p><p>name:String,</p><p>password:String,</p><p>email:String,</p><p>head:String</p><p>},{</p><p>collection:'users'</p><p>});varuserModel=mongoose.model('User',userSchema);functionUser(user){</p><p>this.name=user.name;</p><p>this.password=user.password;</p><p>this.email=user.email;</p><p>};User.prototype.save=function(callback){</p><p>varmd5=crypto.createHash('md5'),</p><p>email_MD5=md5.update(this.email.toLowerCase()).digest('hex'),</p><p>head="http://www.gravatar.com/avatar/"+email_MD5+"?s=48";</p><p>varuser={</p><p>name:this.name,</p><p>password:this.password,</p><p>email:this.email,</p><p>head:head</p><p>};varnewUser=newuserModel(user);newUser.save(function(err,user){</p><p>if(err){</p><p>returncallback(err);</p><p>}</p><p>callback(null,user);</p><p>});</p><p>};User.get=function(name,callback){</p><p>userModel.findOne({name:name},function(err,user){</p><p>if(err){</p><p>returncallback(err);</p><p>}</p><p>callback(null,user);</p><p>});</p><p>};module.exports=User;</p>