全局安装 json-server
npm i -g json-server
检查json-server 是否安装成功
json-server -h
新建 json 文件
新建一个 data.json
的文件
{
"items": [
{
"id": 1,
"name": "周瑜"
},
{
"id": 2,
"name": "黄盖"
},
{
"id": 3,
"name": "黄忠"
},
{
"id": 4,
"name": "大乔"
},
{
"name": "张飞",
"id": 5
},
{
"name": "小乔",
"id": 6
}
]
}
在 data.json
所在的文件目录中,cd server
json-server --watch data.json
服务器启动
本地 json 数据的增删改查
以 axios 发请求
// 查:
axios({
method:"get",
url:"localhost:3000/list"
}).then((data)=>{console.log(data)})
// 增:
axios({
method:"post",
url:"http://localhost:3000/items",
data:{id:5,name:"刘备"}
})
// 删:
axios({
method:"delete",
url:"http://localhost:3000/items/1",
success(res){
console.log(res,"删除成功")
}
})