【前端工具】auto-mock约定式路由mock工具

@winches/auto-mock

github地址https://github.com/winchesHe/auto-mock

约定式路由mock工具

⭐️ feature

✅ auto get mock route (自动获取mock路径)

✅ Hot Update 热更新支持(检测mock路径和配置文件)

🤖 easy to get params(更简易的获取请求参数)

⚡ Faster response build with fastify(更快的响应速度)

Install

npm i @winches/auto-mock -D
# or
yarn add @winches/auto-mock -D
# or
pnpm add @winches/auto-mock -D

Usage

add script

"scripts": {
  "mock": "auto-mock"
},

Create a mock.config.c?js file set some config

module.exports = {
  // default option value
  mockPath: '__mock__', // mock文件路径
  watch: false, // 是否热更新
  port: 5050, // 端口号
}

Run script

npm run mock

运行参数

热更新模式
auto-mock -w

设置端口

auto-mock -p 8080

创建一个本地__mock__文件夹

For instance
└── 存放mock目录(__mock__)
  │
  └──  hello
    │
    └──  index.js

auto-mock会自动将上面mock路径转成请求 http://<host>:<port>/hello/index

index.js暴露接受一个函数,并且携带三个参数,函数的返回值就是mock请求的结果

For instance
// hello/index.js
module.exports = function(req, query, body) {
  // response result
  return {
    code: 0,
    msg: 'success',
    req,
    query,
    body
  }
}

此时将代理设置到auto-mock启动的服务中

发起请求http://host:port/hello/index将得到返回值

{
    code: 0,
    msg: 'success',
    req,
    query,
    body
  }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值