模拟数据 json-server

0. 零

json-server

npm install -g json-server

Create a db.json file with some data

{
  "posts": [
    { "id": 1, "title": "json-server", "author": "typicode" }
  ],
  "comments": [
    { "id": 1, "body": "some comment", "postId": 1 }
  ],
  "profile": { "name": "typicode" }
}
json-server --watch db.json

Now if you go to http://localhost:3000/posts/1, you’ll get

{ "id": 1, "title": "json-server", "author": "typicode" }

1. 增

axios({
    method: 'POST',
    url: 'http://localhost:3000/posts',
    data: {
        title: '快报快报',
        author: 'xxx'
    }
}).then((res) => {
    console.log(res.data);
});

2. 删

axios({
    method: 'DELETE',
    url: 'http://localhost:3000/posts/3'
}).then((res) => {
    console.log(res.data);
});

3. 改

axios({
    method: 'PUT',
    url: 'http://localhost:3000/posts/3',
    data: {
        title: '快报快报~',
        author: 'elser'
    }
}).then((res) => {
    console.log(res.data);
});

4. 查

axios({
    method: 'GET',
    url: 'http://localhost:3000/posts/3'
}).then((res) => {
    console.log(res.data);
});

版权声明:本文为dangpugui原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。