1 Star 0 Fork 0

陈绍华 / mock-hande-api

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

用 JSON-server 模拟 REST API

安装

npm install json-server

查看帮助

json-server -h

json-server [options] <source>

选项:
  --config, -c       Path to config file            [默认值: "json-server.json"]
  --port, -p         Set port                                     [默认值: 3000]
  --host, -H         Set host                                [默认值: "0.0.0.0"]
  --watch, -w        Watch file(s)                                        [布尔]
  --routes, -r       Path to routes file
  --static, -s       Set static files directory
  --read-only, --ro  Allow only GET requests                              [布尔]
  --no-cors, --nc    Disable Cross-Origin Resource Sharing                [布尔]
  --no-gzip, --ng    Disable GZIP Content-Encoding                        [布尔]
  --snapshots, -S    Set snapshots directory                       [默认值: "."]
  --delay, -d        Add delay to responses (ms)
  --id, -i           Set database id property (e.g. _id)          [默认值: "id"]
  --quiet, -q        Suppress log messages from output                    [布尔]
  --help, -h         显示帮助信息                                         [布尔]
  --version, -v      显示版本号                                           [布尔]

示例:
  json-server db.json
  json-server file.js
  json-server http://example.com/db.json

https://github.com/typicode/json-server

配置文件

json-server.json

{
  "host": "0.0.0.0",
  "port": "3003",
  "watch": true
}

启动脚本

package.json

"scripts": {
    "mock": "json-server mock.json"
  }

启动

npm run mock

访问

http://localhost:3003

操作数据

  • GET
$.ajax({
    type: 'get',
    url: 'http://localhost:3003/news'
  }
)
获取所有数据

$.ajax({
    type: 'get',
    url: 'http://localhost:3003/news/1'
  }
)
获取所有数据 id = 1 的数据
  • POST
$.ajax({
    type: 'post',
    url: 'http://localhost:3003/news',
    data: {
      "id": 3,
      "title": "我是新加入的新闻",
      "date": "2019-09-26",
      "likes": 0,
      "views": 0
    }
  }
)

再次 GET 访问可获取新增加数据
  • PUT
$.ajax({
    type: 'put',
    url: 'http://localhost:3003/news/1',
    data: {
      "title": "曹县宣布昨日晚间登日失败",
      "date": "2016-08-12",
      "likes": 55,
      "views": 100086
    }
  }
)

再次 GET 访问可获取修改数据
  • PATCH
同 PUT
  • DELETE
$.ajax({
    type: 'delete',
    url: 'http://localhost:3003/news/1'
  }
)

再次 GET 访问 id = 1 的数据被删除

pm2

pm2 是一个带有负载均衡功能的Node应用的进程管理器

示例

package.json


{
"scripts": {
    "split": "json-server split-file/server.js -r split-file/routes.json"
  }
}
  • node 方式运行项目

npm run split

  • pm2 方式运行项目

pm2 start npm --name mock -- run split

pm2 常用指令
  • 查看所用已启动项目

pm2 list

  • 重启

pm2 restart xxx

  • 停止

pm2 stop xxx

  • 删除

pm2 delete xxx

空文件

简介

汉德供应链小程序 mock 接口。 展开 收起
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/marlboro029/mock-hande-api.git
git@gitee.com:marlboro029/mock-hande-api.git
marlboro029
mock-hande-api
mock-hande-api
master

搜索帮助