JSON-Server
官网:https://npmjs.com/package/json-server
全局安装:
npm install -g json-server
在相对应的目录下,启动json文件(.\db.json文件目录 port 端口号)
json-server --watch .\db.json --port 5000
get 取数据:
//默认为http:localhsot:5000/posts?id=1
axios.get('http:localhsot:5000/posts/1').then(res => {
console.log(res)
})
post 新增数据:
//将对象数据存入http:localhsot:5000/posts,同时id会自增
axios.post('http:localhsot:5000/posts',{
title:'333',
author:'xiaoming'
})
put 修改数据(全局替换):
//在http:localhsot:5000/posts会找到id为1的数据,{}对象为修改数据,会完全替换数据
axios.put('http:localhsot:5000/posts/1',{
title:'1111修改',
})
patch 修改数据(局部替换、更新):
//在http:localhsot:5000/posts会找到id为1的数据,{}对象为修改数据,会局部替换数据,更新数据
axios.patch('http:localhsot:5000/posts/1',{
title:'1111修改',
})
delete 删除数据:
//在http:localhsot:5000/posts会找到id为1的数据,删除(注意:会把关联数据同时一起删除)
axios.delete('http:localhsot:5000/posts/1‘)
_embed 把关联的数据一起显示:
//在http:localhsot:5000/posts中会把comments表中关联的数据一起显示(此时,posts表在comments上面,由上往下寻找)
axios.get('http:localhsot:5000/posts?_embed=comments‘).then(res => {
console.log(res);
})
_expand 把关联的数据一起显示:
//在http:localhsot:5000/comments中会把posts表中关联的数据一起显示(此时,posts表在comments上面,由下往上寻找)
axios.get('http:localhsot:5000/comments?_expand=posts‘).then(res => {
console.log(res);
})