1.确保你安装了monk
npm install monk --save
2.model层如下图所示
33.pngmodel.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
网友评论