1.根目录/module/config.js

/**
* 配置文件
*/
var app = {
dbUrl: 'mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb',
dbName: 'koa'
} module.exports = app;

2.根目录/module/db.js

/**
* DB库
*/
var MongoClient = require('mongodb').MongoClient;
var Config = require('./config.js'); class Db {
/**
* 单例
* 解决多次实例化,实例不共享的问题
*/
static getInstance() {
if (!Db.instance) {
Db.instance = new Db();
}
return Db.instance;
} constructor() {
this.dbClient = ''; /*属性 存放db对象*/
this.connect(); /*实例化的时候就连接数据库*/
} connect() { /*连接数据库*/
let _that = this;
return new Promise((resolve, reject) => {
if (!_that.dbClient) { /*解决数据库多次连接的问题*/
MongoClient.connect(Config.dbUrl, (err, client) => {
if (err) {
reject(err);
} else {
_that.dbClient = client.db(Config.dbName);
resolve(_that.dbClient);
}
})
} else {
resolve(_that.dbClient);
}
})
} find(collectionName, json) {
return new Promise((resolve, reject) => {
this.connect().then((db) => {
var result = db.collection(collectionName).find(json);
result.toArray((err, docs) => {
if (err) {
reject(err);
return;
}
resolve(docs);
})
})
})
} insert() {
//
}
} module.exports = Db.getInstance();

3.根目录/app.js

// 引入模块
const Koa = require('koa');
const router = require('koa-router')(); /*引入是实例化路由 推荐*/
const render = require('koa-art-template');
const path = require('path');
const DB = require('./module/db.js'); // 实例化
let app = new Koa(); // 配置 koa-art-template 模板引擎
render(app, {
root: path.join(__dirname, 'views'), // 视图的位置
extname: '.html', // 后缀名
debug: process.env.NODE_ENV !== 'production' // 是否开启调试模式
}) router.get('/', async (ctx) => {
console.time('start1');
let result = await DB.find('user', {});
console.log(result);
console.timeEnd('start1'); await ctx.render('index', {
list: {
name: '张三'
}
});
}) router.get('/news', async (ctx) => {
console.time('start2');
let result = await DB.find('user', {});
console.log(result);
console.timeEnd('start2'); ctx.body = '新闻页面';
}) app.use(router.routes());
app.use(router.allowedMethods()); app.listen(3000);

.

koa 基础(二十三)封装 DB 库 --- 应用的更多相关文章

  1. koa 基础(二十二)封装 DB 库 --- 测试

    1.根目录/module/config.js /** * 配置文件 */ var app = { dbUrl: 'mongodb://127.0.0.1:27017/?gssapiServiceNam ...

  2. Bootstrap <基础二十三>页面标题(Page Header)

    页面标题(Page Header)是个不错的功能,它会在网页标题四周添加适当的间距.当一个网页中有多个标题且每个标题之间需要添加一定的间距时,页面标题这个功能就显得特别有用.如需使用页面标题(Page ...

  3. koa 基础(二十四)封装 DB 库 --- 新增数据、更新数据、删除数据

    1.根目录/module/db.js /** * DB库 */ var MongoClient = require('mongodb').MongoClient; var Config = requi ...

  4. 学习笔记二十三——字符函数库cctype【转】

    本文转载自: 字符函数库cctype 在头文件cctype(ctype.h)中定义了一些函数原型,可以简化输入确定字符是否为大写字母.数字.标点符号等工作. 例如: 如果ch是一个字母,则isalph ...

  5. koa 基础(十三)koa-art-template 模板引擎的使用

    1.项目目录 2.app.js /** * http://aui.github.io/art-template/koa/ * 1.npm install --save art-template * n ...

  6. Bootstrap <基础二十九>面板(Panels)

    Bootstrap 面板(Panels).面板组件用于把 DOM 组件插入到一个盒子中.创建一个基本的面板,只需要向 <div> 元素添加 class .panel 和 class .pa ...

  7. Bootstrap <基础二十八>列表组

    列表组.列表组件用于以列表形式呈现复杂的和自定义的内容.创建一个基本的列表组的步骤如下: 向元素 <ul> 添加 class .list-group. 向 <li> 添加 cl ...

  8. Bootstrap<基础二十七> 多媒体对象(Media Object)

    Bootstrap 中的多媒体对象(Media Object).这些抽象的对象样式用于创建各种类型的组件(比如:博客评论),我们可以在组件中使用图文混排,图像可以左对齐或者右对齐.媒体对象可以用更少的 ...

  9. Bootstrap <基础二十六>进度条

    Bootstrap 进度条.在本教程中,你将看到如何使用 Bootstrap 创建加载.重定向或动作状态的进度条. Bootstrap 进度条使用 CSS3 过渡和动画来获得该效果.Internet ...

随机推荐

  1. ubuntu系统新用户添加

    大概是4个步骤吧,是用脚本实现的,这里我列一下关键点 sudo useradd -m userYouWantAdd sudo passwd userYouWantAdd sudo usermod -a ...

  2. 转载Linux常用命令

    转自:https://blog.csdn.net/deng_xj/article/details/88803148 Linux常用shell命令 [root@dengxj]#各项含义[用户名@计算机名 ...

  3. odoo 字段组件

    每个字段类型都会使用相应的默认组件在表单中显示.但还有一些替代组件可以使用.对于文本字段,有如下组件: email用于让 email 文本成为可操作的”mail-to”地址 url用于将文本格式化为可 ...

  4. 单节点FastDFS与Nginx部署

    一.安装基本组件 1.安装编译需要的组件,必安装组件. yum install gcc-c++ 2.安装libevent函数库.pcre-devel zlib-devel必安装组件.     yum ...

  5. three.js之让物体动起来方式(一)移动摄像机

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. 第1章 python入门

    1.1 python的出生与应用   python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆(中文名字:龟叔)为了在阿姆斯特丹打发时间,决心开 ...

  7. Arduino短学期作业展示

    自己挖的坑终于填上了,真是欣慰啊= = 源代码:https://github.com/Miyeah/Arduino-Dormitory-Assistant Arduino-Dormitory-Assi ...

  8. QTP(2)

    注意: 在使用QTP录制代码时,能使用鼠标点击的就不要使用键盘操作,能单击的操作就不要使用双击 一.QTP的工作流程 1.录制测试脚本前的准备: a.分析被测系统是否可以实现自动化测试 b.分析被测系 ...

  9. python 示例代码1

    第一章 python基础一 ​在此不再赘述为什么学习python这门编程,网上搜索一箩筐.我在此仅说一句python的好,用了你就会爱上它. 本python示例代码1000+带你由浅入深的了解pyth ...

  10. 安装WIN10+Ubuntu18.04安装教程(实测有效)

    转载原文链接:https://www.cnblogs.com/masbay/articles/10745170.html 安装过程中尤其注意分区时候的挂载点一定要选对!!!选择Ubuntu的EFI所在 ...