Vue.js进阶【5-3】mountebank入门

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ClamReason/article/details/82763234

mountebank

mountebank(江湖骗子、江湖医生):是一个给前端测试用的API服务模拟工具,无需服务端,你就可以干活了。

安装:

C:\windows\system32>cnpm install -g mountebank

运行:

C:\windows\system32>mb

 加载配置文件运行:

imposters.ejs是配置文件,用来配置模拟的请求的返回值。

注意:配置文件更新后,需需要重启命令,暂时未找到热更新方法

mb start --configfile ./imposters.ejs

配置文件要写成绝对路径 :

C:\windows\system32>mb start --configfile D:\Vue\vue-router-test\pos\mountebank-test-http-api.json

还可以在当前目录启动命令行,配置路径写成./

D:\Vue\vue-router-test\pos>mb start --configfile ./mountebank-test-http-api.json

配置文件:

{
  "port": 4545,
  "protocol": "http",
  "stubs": [
    {
      "predicates": [{"equals": { "path": "/country", "method": "GET" }}],
      "responses": [{
          "is": 
          {
            "statusCode": 200,
            "headers": {"Content-Type": "application/json"},
            "body": ["Australia", "Brazil", "Canada", "Chile", "China", "Ecuador", "Germany", "India", "Italy", "Singapore", "South Africa", "Spain", "Turkey", "UK", "US Central", "US East", "US West"]
          }
      }]
    }, 
    {
      "responses": [{
          "is": {
            "statusCode": 400,
            "body": {
              "code": "bad-request",
              "message": "Bad Request"
            }
          }
      }]
  }]
}

浏览器访问测试:http://localhost:4545/country

浏览器访问测试:http://localhost:4545/country_wrong

大功告成,极大的方便了前端测试,再也不用考虑和后端对接了。

修改完json配置文件记得重启哦!!!

猜你喜欢

转载自blog.csdn.net/ClamReason/article/details/82763234
5-3
今日推荐