美文网首页
006.mongodb的模块开发

006.mongodb的模块开发

作者: cmd_ts | 来源:发表于2019-01-23 15:57 被阅读7次

    1.确保你安装了monk

    npm install monk --save

    2.model层如下图所示

    33.png
    model.js
    const config = require('../config/config');
    
    let db_url = ''
    if(config.debug == false)
    {
        db_url = config.db_test_url;
    }
    else
    {
        db_url = config.db_online_url;
    }
    const url = db_url + '/' + config.db_table;
    
    const db = require('monk')(url);
    
    module.exports = db;
    
    db.js
    const db_users = require('./db/db_users');
    
    module.exports = {
        db_users
    }
    
    db_users.js
    const db = require('../model');
    
    const users = db.get('test');
    
    class db_user{
      async user_find(){
        let haha = await users.find({})   
        return haha;
      }  
    }
    
    var userdb = new db_user();
    
    module.exports = userdb;
    

    运用:

    var router = require('koa-router')();
    const db = require('../../model/db')
    
    router.get('/', async (ctx)=> {
      var temp = await db.db_users.user_find();
      ctx.body = temp;
      console.log(temp)
      //db_users.user_find();
    });
    
    router.get('/cmd', function (ctx) {
      ctx.body = 'this a index/hi response!';
    });
    
    router.post('/denglu', function (ctx, next) {
      ctx.body = ctx.request.body
      console.log('123')
    })
    
    module.exports = router;
    
    444.png

    相关文章

      网友评论

          本文标题:006.mongodb的模块开发

          本文链接:https://www.haomeiwen.com/subject/scxsjqtx.html