const express=require('express')
const fs=require('fs')
const app=express()
app.use(express.urlencoded())
app.listen(2024,()=>{
?? ?console.log('服务器已开启,基准地址:http://localhost:2024')
})
app.get('/search',(req,res)=>{
?? ?const {name}=req.query
?? ?let data=fs.readFileSync('./db/goods.json','utf-8')
?? ?let result=JSON.parse(data).filter((item)=> {
?? ??? ?return item.name.includes(name)
?? ??? ?})
?? ?if(result.length){
?? ??? ?res.send({code: 1, message: '查询成功', data: result})
?? ?}
?? ?else{
?? ??? ?res.send({code: 0, message: '查询不到数据'})
?? ?}
})
app.post('/login',(req,res)=>{
?? ?const {username,password}=req.body
?? ?console.log(username)
?? ?let data=fs.readFileSync('./db/user.json','utf-8')
?? ?let result=JSON.parse(data).find(item=>item.username==username&&item.password==password)
?? ?console.log(result)
?? ?if(result){
?? ??? ?res.send({code: 1, message: '登录成功'})
?? ?}else{
?? ? res.send({code: 0, message: '用户名或密码错误'})
?? ?}
})