lianxi-wh

1.0.5 • Public • Published
const express=require("express");
const path=require("path");
const url=require("url");
const fs=require("fs");
const app=express();
//静态
app.use(express.static("public",{
    index:"index.html"
}));
//接收post端口传过来的数据
app.use(express.json());


//处理登录接口
app.post("/api/login",(req,res)=>{
    //数据复制给body
    let{user,pwd}=req.body;
    //模块引进数据
    let data=require("./mock/use.json");
    //判断flag
    let flag=data.some(item=>item.user===user&&item.pwd===pwd);
    if(flag){
        res.send("1");
    }else{
        res.send("0");
    }
})



//数据查询接口
app.get("/api/list",(req,res)=>{
    //读取数据转json赋值给data
    let data=JSON.parse(fs.readFileSync("mock/list.json"));
    //最后数组
    res.send(data);
})



//删除数据
app.post("/api/delete",(req,res)=>{
    //id进body里面
    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/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.post("/api/search",(req,res)=>{
    let {val}=req.body;
    let data=JSON.parse(fs.readFileSync("mock/list.json"));
    let arr=data.filter(item=>item.name.includes(val));
    res.send(arr);
})
app.listen(5566,()=>console.log("服务器正在启动..."));

Readme

Keywords

none

Package Sidebar

Install

npm i lianxi-wh

Weekly Downloads

0

Version

1.0.5

License

ISC

Unpacked Size

195 kB

Total Files

13

Last publish

Collaborators

  • whwhwh