在学习Vue的过程中,难免会用到Ajax/Axios与后端交互,为方便学习,可以使用JSON Server这款工具模拟后端Http接口。
JSON Server资料
https://github.com/typicode/json-server
快速入门
按照github官方文档操作:
1、安装JSON Server
npm install -g json-server
2、创建文件:db.json
选择项目目录中创建文件即可,并按按示例初始化数据。
{
"posts": [
{ "id": 1, "title": "json-server", "author": "typicode" }
],
"comments": [
{ "id": 1, "body": "some comment", "postId": 1 }
],
"profile": { "name": "typicode" }
}
3、启动JSON Server
json-server --watch db.json 注意db.json的路径
4、验证
浏览器打开地址:http://localhost:3000/posts/1,会看到如下数据:
{ "id": 1, "title": "json-server", "author": "typicode" }
看到上面数据,表示入门成功。