1️⃣ 使用json-server搭建REST API

2️⃣ json-server是什么?使用json-server

在线文档: https://github.com/typicode/json-server
下载: npm install -g json-server
目标根目录下创建数据库json文件: db.json

  1. {
  2. "posts": [
  3. { "id": 1, "title": "json-server1", "author": "typicode1" }
  4. { "id": 2, "title": "json-server2", "author": "typicode2" }
  5. { "id": 3, "title": "json-server3", "author": "typicode3" }
  6. ],
  7. "comments": [
  8. { "id": 1, "body": "some comment1", "postId": 1 }
  9. { "id": 2, "body": "some comment2", "postId": 2 }
  10. { "id": 3, "body": "some comment3", "postId": 3 }
  11. ],
  12. "profile": { "name": "typicode" }
  13. }

启动服务器执行命令: json-server —watch db.json

2️⃣ 使用浏览器访问测试

http://localhost:3000/posts
http://localhost:3000/posts/1

2️⃣ 使用postman测试接口

测试GET/POST/PUT/DELETE请求