美文网首页
Json-Server模拟后台数据接口

Json-Server模拟后台数据接口

作者: real_ting | 来源:发表于2018-04-04 18:31 被阅读0次

开发环境:

需要安装 Node.js (https://nodejs.org/en/)

开始:

1. 新建项目并初始化项目 npm init

2. 安装:npm install --save-dev json-server

3. 创建一个json文件,格式类似
   
  {
     "user": [
         {"id": 1, "userName": "tt", "password": "1125" }
     ],
    "content": [
        {"id": 1, "des": "a girl", "liking": "singing reading and coding" }
    ],
    "friends": [
      {"id": 1, "name": "tom", "age": "18" },
      {"id": 2, "name": "tina", "age": "20" },
      {"id": 3, "name": "hell", "age": "16" }
    ]
 }
    
4. 在package.json文件的启动项中添加

    "server": "json-server data.json"

5.在终端中输入npm server 或 npm run server , 生成以下3个接口

  * http://localhost:3000/user

  * http://localhost:3000/content

  * http://localhost:3000/friends

 6. 打开 localhost:3000, 显示如下内容,即为启动成功
TIM截图20180213162159.png
 7. 访问 [http://localhost:3000/friends](http://localhost:3000/friends), 可得到如下数据
    [
        {
          "id": 1,
          "name": "tom",
          "age": "18"
        },
        {
         "id": 2,
         "name": "tina",
         "age": "20"
        },
       {
        "id": 3,
        "name": "hell",
        "age": "16"
       }
  ]
8. 可进行 get, post, delete, put 操作,可使用 postman 测试

example源码:https://github.com/sunshine1125/json-server
github地址: https://github.com/typicode/json-server

相关文章

网友评论

      本文标题:Json-Server模拟后台数据接口

      本文链接:https://www.haomeiwen.com/subject/llllhftx.html