lianxi-fen
v1.0.5
Published
```js const express=require("express"); const app=express(); const fs=require("fs"); const path=require("path"); const url=require("url"); app.use(express.static("public",{ index:"res.html" })) app.use(express.json()); //登录 app.post("/api/l
Downloads
1
Readme
const express=require("express");
const app=express();
const fs=require("fs");
const path=require("path");
const url=require("url");
app.use(express.static("public",{
index:"res.html"
}))
app.use(express.json());
//登录
app.post("/api/login",(req,res)=>{
let{user,pwd}=req.body;
let data=JSON.parse(fs.readFileSync("./mock/use.json"));
let flag=data.find(item=>item.user===user&&item.pwd===pwd);
if(flag){
res.send({
code: 1,
mess: "登录成功",
id: flag.id,
})
}else{
res.send("0");
}
})
//注册
app.post("/api/res",(req,res)=>{
let{user,pwd}=req.body;
let data=JSON.parse(fs.readFileSync("./mock/use.json"));
let flag=data.find(item=>item.user===user&&item.pwd===pwd);
if(flag){
res.send("1");
}else{
res.send("0");
}
})
//添加数据
app.post("/api/add",(req,res)=>{
let data=JSON.parse(fs.readFileSync("./mock/list.json"));
data.push(req.body);
fs.writeFileSync("./mock/list.json",JSON.stringify(data));
res.send({code:1});
})
//查询数据
app.get("/api/list",(req,res)=>{
let data=JSON.parse(fs.readFileSync("./mock/list.json"));
res.send(data);
})
//删除数据
app.post("/api/del",(req,res)=>{
let {id}=req.body;
let data=JSON.parse(fs.readFileSync("./mock/list.json"));
data=data.filter(item=>item.id!==id);
fs.writeFile("./mock/list.json",JSON.stringify(data),(err)=>{
if(err){
res.send({
code:0,
mess:"删除失败",
})
return;
}
res.send({
code:1,
mess:"删除成功",
})
})
})
//分页
app.post("/api/lis",(req,res)=>{
let {index,num}=req.body;
let data=JSON.parse(fs.readFileSync("./mock/list.json"));
let len=Math.ceil(data.length/num);
let star=index*num;
let end=(index+1)*num;
res.send({
code:1,
maxlen:len,
arr:data.slice(star,end),
});
})
app.listen(5577,()=>console.log("服务器正在启动"));