1. 先安装
npm install json-server -g
2.查看是否安装成功
json-server -h
3.准备数据,新建一个文件夹 mock,cd mock,在mock下 新建test.json
1 { 2 "list": [ 3 { 4 "id": 1, 5 "title": "bim项目启动了", 6 "date": "2018-08-12", 7 "likes": 33, 8 "views": 124 9 }, 10 { 11 "id": 2, 12 "title": "bim项目启动了", 13 "date": "2018-08-12", 14 "likes": 5554, 15 "views": 212 16 } 17 ], 18 "comments": [ 19 { 20 "id": 1, 21 "news_id": 1, 22 "data": [ 23 { 24 "id": 1, 25 "content": "测试数据" 26 }, 27 { 28 "id": 2, 29 "content": "测试数据" 30 } 31 ] 32 } 33 ] 34 }
4.在mock下执行如下代码 -p是指定端口
json-server -w test.json -p 8001
5.然后访问 http://localhost:8001/db 可以查看 test.json 文件中所定义的全部数据