node.js和MySQL留言本_node.js结合mongodb数据库制作留言本

node.js结合mongodb数据库制作留言本

Ken Ken Coding 7月30日

留言本已经是无处不在了,最近学习了node.js之后,想想要做一个留言本

其实很简单,在做之前,我们要规划一下,留言的步骤

1 首先呢,必须要有一个留言的页面,就如下面:(布局很丑,不管了,将就着看吧)

2 留言提交到哪里去呢,当然是数据库啦,这里的数据库是mongodb,是一个非关系型的数据库,支持key-value键值对存储的数据库,在我的理解,其实就是以对象的形式储存把,这是他命令行工具,也就是客户端

3 就是把储存在数据库的留言取出来,放在页面中

在此之前,我对数据库的操作api进行了一次封装 doa.js

/**

* 封装一个类

* @url 数据库链接地址

* @collectionName 集合的名称

*

* _connect()函数用于查询链接数据库的

* 返回一个promise对象

*

* insert(document,insertMany)函数用于插入文档

*

* query(document)查询文档

*

* del(query,deleteMany)删除文档

*

* update(filter,updater)更新文档

*/

当然,你要用npm install mongodb --save-dev;安装一个依赖库

实现代码如下

01

class Doa{

constructor(url,dbName,collectName){

this.url = url;

this.dbName = dbName;

this.collectName = collectName;

}

_connect(){

return new Promise((resolve,reject)=>{

mongo.connect(this.url,(err,client)=>{

if(err){

return reject(err);

}else{

resolve(client);

}

})

})

}

insert(documents,insertMany){

return new Promise((resolve,reject)=>{

this._connect().then(client=>{

// console.log('数据库连接成功');

if(insertMany){

// 插入多条数据

// insertMany是一个布尔值

client.db(this.dbName).collection(this.collectName).insertMany(documents).then(res=>{

resolve(res);

client.close();

}).catch(e=>{

throw new Error(e);

})

return;

}else{

// 插入单条数据

client.db(this.dbName).collection(this.collectName).insertOne(documents).then(res=>{

resolve(res);

client.close();

}).catch(e=>{

throw new Error(e);

})

}

})

})

}

query(documents={},pageConfig={count:0,page:0}){

return new Promise((resolve,reject)=>{

this._connect().then(client=>{

let arr = [];

// 查询文档

let cursor = client.db(this.dbName)

.collection(this.collectName)

.find(documents)

.limit(pageConfig.count)

.skip((pageConfig.page-1) * pageConfig.count);

// console.log(cursor);

// each查询每一条数据

cursor.each((err,data)=>{

// console.log(data);

if(err){

// 如果存在错误

// 直接放回

reject(err);

}

// data最后一条数据为null

// 以此判断依据

if(data!==null){

// 将每一条数据放入数组中

arr.push(data);

}else{

// 当data为null时把数据返回出去

// 并且关闭数据库

resolve(arr);

client.close();

}

})

// resolve(cursor);

}).catch(e=>{

reject(e);

})

})

}

del(query,deleteMany){

// 步骤跟insert()函数相似

return new Promise((resolve,reject)=>{

this._connect().then(client=>{

if(deleteMany){

client.db(this.dbName)

.collection(collectName)

.deleteMany(query)

.then(res=>{

resolve(res);

client.close();

}).catch(e=>{

reject(e);

})

}else{

client.db(this.dbName)

.collection(collectName)

.deleteOne(query)

.then(res=>{

resolve(res);

client.close();

}).catch(e=>{

reject(e);

})

}

}).catch(e=>{

reject(e);

})

})

}

update(filter,updater){

return new Promise((resolve,reject)=>{

this._connect().then(client=>{

let updaterCopy = {$set:updater};

client.db(this.dbName)

.collection(this.collectName)

.updateMany(filter,updaterCopy)

.then(res=>{

resolve(res);

client.close();

}).catch(e=>{

reject(e);

});

}).catch(e=>{

reject(e);

})

})

}

}

接下来实现后台代码:

我们来看一下我的留言本目录吧:

看一下向导吧

最终浏览的效果是

源码已经放在我的github地址上啦

https://github.com/KenNaNa/mongoose

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值