yz-add-del
v1.0.0
Published
```javascript const express = require("express"); const router = express.Router(); const fs = require("fs"); module.exports = router;
Downloads
5
Readme
#一个添加删除修改
const express = require("express");
const router = express.Router();
const fs = require("fs");
module.exports = router;
router.post("/api/login",(req,res)=>{
//数据
let {user,pass} = req.body;
//当前数据取
let data= JSON.parse(fs.readFileSync("./mock/user.json"));
//数据判断
let flag= data.find(item=>item.user===user && item.pass===pass);
//判断
if(flag){
res.send({
code:1,
mess:"登录成功",
})
}else{
res.send({
code:0,
mess:"账号密码不对",
})
}
})
const express = require("express");
const router = express.Router();
const fs = require("fs");
module.exports = router;
//删除的接口
router.post("/api/delete",(req,res)=>{
//结束数据
let {id} =req.body;
//获取数据
let data =JSON.parse(fs.readFileSync("./mock/list.json"));
//判断数据里面的id进行筛选
let flag=data.filter(item=>+item.id !== +id);
//重新写入
fs.writeFileSync("./mock/list.json",JSON.stringify(flag));
//fhui
res.send({
code:1,
})
})
//添加的接口
router.post("/api/add",(req,res)=>{
//读取数据
let data =JSON.parse(fs.readFileSync("./mock/list.json"));
//储存随机id
req.body.id =Math.random().toString().slice(2);
//推入数组
data.unshift(req.body);
//写入数据
fs.writeFileSync("./mock/list.json",JSON.stringify(data));
//返回
res.send({
code:1,
})
})
//修改编辑的接口
router.post("/api/modfiy",(req,res)=>{
//获取修改的数据
let{id}=req.body;
//获取数据
let data =JSON.parse(fs.readFileSync("./mock/list.json"));
//判断修改的id
let obj = data.find(item=>item.id==id);
//赋值
Object.assign(obj,req.body);
//重新写入数据
fs.writeFileSync("./mock/list.json",JSON.stringify(data));
//返回
res.send({
code:1,
})
})