json-server,一个接口模拟器

284 阅读1分钟

作用

快速搭建测试接口

依赖

  • node环境

操作步骤

1.安装

cnpm i json-server -g

2.创建一个a.json,该文件包含需要返回的数据

{
    "posts": [
        {"id": 1,"title": "写一篇文章,很久没写了,回忆下","author": "张三"},
        {"id": 2,"title": "写两篇文章,很久没写了,回忆下","author": "张三"}
    ],
    "comments": [
        {"id": 1,"body": "666"}
    ],
    "profile": {
        "name": "888"
    }
}

3.启动

json-server --watch a.json 

4.访问

http://localhost:3000/posts/1    
链接解析:本地3000端口下,pos下第一条

5.数据筛选、分页、排序

其他用法查主页,开发阶段当做模拟数据使用,也可以本地加载json文件,又快又方便还少运行服务,着实没必要挖这么深,拙见END。