开发模拟 API 环境搭建

发布时间: 更新时间: 总字数:93 阅读时间:1m 作者: IP上海 分享 网址

使用 json-server 搭建开发 REST API 环境

安装

npm install -g json-server

使用

  • 创建 db.json
{
  "posts": [
    { "id": 1, "title": "json-server", "author": "typicode" }
  ],
  "comments": [
    { "id": 1, "body": "some comment", "postId": 1 }
  ],
  "profile": { "name": "typicode" }
}
  • 启动
json-server --watch db.json
  • demo
$ curl http://localhost:3000/posts/1
{ "id": 1, "title": "json-server", "author": "typicode" }

其他方法如 Post、Put、Delete 也支持。

参考

  1. https://github.com/typicode/json-server
  2. https://github.com/ant0ine/go-json-rest
Home Archives Categories Tags Statistics
本文总阅读量 次 本站总访问量 次 本站总访客数