db.js
// 引入模块
var mongodb = require("mongodb");
// 创建对象,用来连接数据
var MongoClient = mongodb.MongoClient;
// 数据库地址
var dbUrl = "mongodb://localhost:27017";
// 连接数据库的名称
var dbName = "my2107";
// 关联数据库的封装
function connectMGDB (res, cb) {
MongoClient.connect(dbUrl, { useNewUrlParser: true }, function (err, db) {
if (err) {
console.log("连接数据库失败");
res.send({ code: 404, msg: "数据库连接失败" })
} else {
console.log("连接数据库成功");
// 关联数据库
var dbase = db.db(dbName);
// 回调函数
cb(dbase, db)
}
})
}
// 插入一条数据{}
module.exports.insertOne = function (res, obj, cName, cb) {
// 要存放的数据是传过来obj
// 调用数据库
connectMGDB(res, function (dbase, db) {
dbase.collection(cName).insertOne(obj, function (err, result) {
cb(err, result, db);
})
})
}
// 插入多条数据[]
module.exports.insertMany = function (res, arr, cName, cb) {
// 要存放的数据是传过来arr
// 调用数据库
connectMGDB(res, function (dbase, db) {
dbase.collection(cName).insertMany(arr, function (err, result) {
cb(err, result, db);
})
})
}
// 删除一条数据
module.exports.deleteOne = function (res, obj, cName, cb) {
// 要删除的数据是传过来obj
// 调用数据库
connectMGDB(res, function (dbase, db) {
dbase.collection(cName).deleteOne(obj, function (err, result) {
cb(err, result, db);
})
})
}
// 删除多条数据
module.exports.deleteMany = function (res, obj, cName, cb) {
// 要删除的数据是传过来obj
// 调用数据库
connectMGDB(res, function (dbase, db) {
dbase.collection(cName).deleteMany(obj, function (err, result) {
cb(err, result, db);
})
})
}
// 修改一条数据
module.exports.updateOne = function (res, whereObj, updateObj, cName, cb) {
// 要查询的数据是传过来whereObj
// 要修改的数据是传过来updateObj
// 调用数据库
connectMGDB(res, function (dbase, db) {
dbase.collection(cName).updateOne(whereObj, updateObj, function (err, result) {
cb(err, result, db);
})
})
}
// 修改多条数据
module.exports.updateMany = function (res, whereObj, updateObj, cName, cb) {
// 要查询的数据是传过来whereObj
// 要修改的数据是传过来updateObj
// 调用数据库
connectMGDB(res, function (dbase, db) {
dbase.collection(cName).updateMany(whereObj, updateObj, function (err, result) {
cb(err, result, db);
})
})
}
// 查找多条数据
module.exports.find = function (res, whereObj, cName, cb) {
// 要查询的数据是传过来whereObj
whereObj.find ? whereObj.find : whereObj.find = {};
whereObj.sort ? whereObj.sort : whereObj.sort = {};
whereObj.limit ? whereObj.limit : whereObj.limit = 0;
whereObj.skip ? whereObj.skip : whereObj.skip = 0;
// 调用数据库
connectMGDB(res, function (dbase, db) {
dbase.collection(cName).find(whereObj.find).sort(whereObj.sort).limit(whereObj.limit).skip(whereObj.skip).toArray(function (err, result) {
cb(err, result, db);
})
})
}
// 查询数据条数
module.exports.count = function (res, whereObj, cName, cb) {
// 要查询的数据是传过来whereObj
// 调用数据库
connectMGDB(res, function (dbase, db) {
dbase.collection(cName).count(whereObj).then(function (count) {
cb(count, db);
})
})
}
// 删除集合(全部删除)
module.exports.count = function (res, whereObj, cName, cb) {
// 要查询的数据是传过来whereObj
// 调用数据库
connectMGDB(res, function (dbase, db) {
dbase.collection(cName).drop(function (err, bool) {
cb(err, bool, db);
})
})
}
server.js
//引入模块
const express = require("express");
const bodyParser =require("body-parser");
const cors =require("cors");
const fs = require("fs");
const app =express();
//实例化
app.use(cors());
app.use(express.static("public"));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:true}));
//获取渲染数据
app.get("/list",(req,res) =>{
fs.readFile(__dirname + "/public/data/data.json","utf-8",function(err,data){
if(err) {
console.log("渲染接口错误",err);
}else{
res.send(JSON.stringify({code:200,msg:"渲染成功",info:JSON.parse(data)}));
}
})
})
//获取添加数据
app.post("/add",(req,res) =>{
fs.readFile(__dirname + "/public/data/data.json","utf-8",function(err,data){
//将JSON文件数据拿出来转JSON
var arr=JSON.parse(data);
//接收前端传参
var obj=req.body;
//判断前端字段是否正确
if(obj.user && obj.age) {
//添加id
obj.id = new Date().getTime();
arr.push(obj);
console.log(arr);
fs.writeFile(__dirname + "/public/data/data.json",JSON.stringify(arr),function(err,data){
if(err) {
//后端提示错误
console.log("添加写入错误",err);
//给前端返回错误
res.send(JSON.stringify({code:404,msg:"后端添加写入失败"}))
}else{
//后端写入成功
console.log("后端写入成功");
//给前端反馈提示
res.send(JSON.stringify({code:200,msg:"添加成功"}))
}
})
}else{
//给前端反馈提示
res.send(JSON.stringify({code:200,msg:"字段错误"}));
}
})
})
//删除
app.get("/del",(req,res) =>{
fs.readFile(__dirname + "/public/data/data.json", "utf-8", function(err,data){
var arr =JSON.parse(data);
var id = req.query.id;
if(id){
var index = arr.findIndex(function(item){
return item.id = id;
})
arr.splice(index,1);
fs.writeFile(__dirname + "/public/data/data.json",JSON.stringify(arr),function(err){
if(err){
//后端提示错误
console.log("删除写入失败",err);
//传到前端
res.send(JSON.stringify({code: 404,msg:"后端删除写入失败"}));
}else{
console.log("删除写入成功");
res.send(JSON.stringify({code:200,msg:"删除成功"}))
}
})
}else{
res.send(JSON.stringify({code:404,msg:"前端字段错误"}))
}
})
})
//监听
app.listen(8080, () =>{
console.log("server is running at http://localhost8080")
})