11-3-address-ly 中文文档教程
//app.js const express=require('express'); const app=express(); const fs=require('fs');
const loginRouter=require('./server/login.js'); const listRouter=require('./server/list.js');
app.listen(8888,()=>{ console.log('开启'); })
app.use(express.static('public',{ index: "login.html" }))
app.use(express.json());
app.use(loginRouter); app.use(listRouter);
//listRouter.js const express=require('express'); const router=express.Router(); const fs=require('fs'); module.exports=router;
router.post('/aaa/list',(req,res)=>{ res.send(fs.readFileSync('mock/list.json')); })
router.post('/aaa/add',(req,res)=>{ let {id,name,tel,address,flag}=req.body; let data=JSON.parse(fs.readFileSync('mock/list.json')); if(flag){ data.unshift({ name,tel,address,id }) }else{ data.push({ name,tel,address,id }) } fs.writeFileSync('mock/list.json',JSON.stringify(data)); res.send({code:1}); })
//修改数据 router.post('/aaa/modify',(req,res)=>{ let {id}=req.body; // console.log(req.body); let data=JSON.parse(fs.readFileSync('mock/list.json')); 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}); })
//删除数据 router.post('/aaa/delete',(req,res)=>{ let {id}=req.body; // console.log(req.body); let data=JSON.parse(fs.readFileSync('mock/list.json')); data=data.filter(item=>item.id!==id); fs.writeFileSync('mock/list.json',JSON.stringify(data)); res.send({code:1}); })
//loginRouter.js const express=require('express'); const router=express.Router(); const fs=require('fs'); module.exports=router;
router.post('/aaa/login',(req,res)=>{ let {username,password}=req.body; let data=JSON.parse(fs.readFileSync('mock/user.json')); let flag=data.some(item=>item.username===username&&item.password===password); if(flag){ res.send({code:1}); }else{ res.send({code:0}); } })