Giter Club home page Giter Club logo

easy-mock's Introduction

@kxgo/easy-mock

easy-mock只需一些简单的配置就可以轻松mock数据

安装


npm i @kxgo/easy-mock
or
yarn add @kxgo/easy-mock

使用


const path = require('path')
const easyMock = require('@kxgo/easy-mock')

new easyMock({
  // 请传递完整的配置文件路径进去,服务是会自动刷新
  path: path.resolve(__dirname, 'xxxx.js')
})

配置文件的例子

// xxxx.js
module.exports = {
  // common选项不是必须, 内置的配置如下,当然你也可以更改
  common: {
    // mock服务的默认端口,如果端口被占用,会换一个
    port: 8018,
    // 如果你想看一下ajax的loading效果,该配置项可以设置接口的返回延迟
    timeout: 500,
    // 如果你想看一下接口请求失败的效果,将rate设置成0就可以了,rate取值范围0~1,代表成功的概率
    rate: 1,
    // mock属性为false的话可以关闭掉mock服务
    mock: true,
    // 是否打印调试信息
    debug: false
  },
  // api...,可以不用写全,写最后一个就好
  'getList': {
    code: 0,
    'data|5': [{
      'uid|1000-99999': 999,
      'name': '@cname'
    }],
    result: true
  },
  '/pkApi/getPerson': {
    code: 0,
    'data|5': [{
      'name': '@cname'
    }],
    result: true
  },
  '/pkApi/getOther': {
    code: 0,
    'data|5': [{
      'name': '@cname'
    }],
    result: true
  }
}

服务启动后,打开http://127.0.0.1:8018/pkApi/getPerson就可以看到mock的数据了!

本地debug

npm i kxg -g
npm i nodemon -g

npm run dev

发布

npm i standard-version -g
npm run pub

Q&A


1 怎么去mock jsonp

在链接后面传callback参数就好了

2 怎么去mock数据

参考mock rule

3 怎么去处理更复杂的场景,我想要自定义数据返回
module.exports = {
  ...
  ['/pkApi/getOther'] (req, res) {
    const query = req.query
    // 可以自己写处理逻辑
    return {}
  }
  ...
}

get类型的请求可以通过req.query去获取参数

post类型的请求可以通过req.body去获取参数

issues


issue list

changelog

changelog

easy-mock's People

Watchers

James Cloos avatar Robbie avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.