Loading...

son-server模拟http mock数据

json-server

前端开发中,想通过异步请求服务端json数据,但是服务端还没有开发完,此时可以快速启动一个server服务

1,安装json-server插件

npm -g add json-server

2,创建一个json文件 test.json

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

3,启动server服务

json-server --watch data.json  --port 3004  /*指定端口启动启动*/

4,son-server默认的静态资源(HTML,CSS,JS等)是在与json数据文件同级目录下的public文件夹中,你只需要创建一个名为public的文件夹,把静态资源放到public目录下,然后直接运行以下命令启动即可:

json-server data.json 

5,假如我们需要指定静态资源文件夹的位置,则可以通过指定目录来启动json-server即可,如指定静态资源为 json 数据同级目录的source文件夹下,则:

json-server data.json --static ./source

  

posted @ 2019-07-01 14:12  微笑阳光哈*_*  阅读(293)  评论(0编辑  收藏  举报