码迷,mamicode.com
首页 > 数据库 > 详细

koa1链接mongodb

时间:2018-02-02 14:27:25      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:route   host   glob   new   UI   高效   pwd   query   cal   

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;

})

 

koa1链接mongodb

标签:route   host   glob   new   UI   高效   pwd   query   cal   

原文地址:https://www.cnblogs.com/yu-hailong/p/8404465.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!