JSON-Server

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);
})
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值