koa1链接mongodb
1.项目下安装mongodb和mongoose
npm install mongodb --save-dev
npm install mongoose --save-dev
2.router中
var router = require('koa-router')(); var mongoose = require('mongoose'); mongoose.Promise = global.Promise; //初始化 //mongoose.Promise = require('bluebird'); //疑似高效 //----------------希望建个module文件放在里面 var db = mongoose.connect('mongodb://localhost/mydb'); var User = mongoose.model('user', new mongoose.Schema({ email: String, pwd: String, nicheng: String, },{_id:true})); //====================================== router.post('/zhuce', function *(next) { let subflag = this.query['subflag']; let email = this.request.body['email']; //this.body = '收到email:'+email+',get收到:'+subflag; var user = new User({uname:'dd',pwd:'dd'}); user.nicheng='张三'; user.age=20; //无效 yield user.save(); this.body = user._id; })