mouk-server

a rest api mock server that help you start a rest-api-server in seconds

Usage no npm install needed!

<script type="module">
  import moukServer from 'https://cdn.skypack.dev/mouk-server';
</script>

README

mouk server

a rest api mock server that help you start a rest-api-server in seconds

installation

npm install -g mouk-server

or with yarn

yarn global add mouk-server

configuration

mouk server will find api.js in current directory as configuration

api.js may looks like:

module.exports = {
  apis: [
    {
      url: "/user/info",
      method: "GET",
      wait: 3000,
      expr:{
        "gender|1": [0, 1],
        nickname: "@cname",
        username: "@word(6)"
      }
    },
  ]
}

Define your api route in apis array, one by one.

Route options:

option type desc
url string api entry
method string rest method, GET & POST supported currently
wait number api response wait time (in millisecond)
expr object response body definition, see also [mockjs doc]
fn function data generate function, mockjs instance will passed as first argument, if fn is present, expr will be ignored

*ps: expr is more like a static way to define the shape of your response, if your requirement is way more complicate, you can consider using fn to return any kind of response data (even with logic)

use fn to generate response data

{
  url: "/data/with/fn",
  method: "GET",
  fn: (mockjs) => {
    const { cols } = mockjs.mock({ "cols|10": [{ "name": "@cword(1,3)", "props": "@word(3,5)" }] })
    const data = (new Array(10)).fill(-1).map(row => {
      const dataRow = {}
      cols.forEach(col => {
        dataRow[col.props] = mockjs.mock('@integer(0, 1000)')
      })
      return dataRow
    })
    return { name: mockjs.mock("@cword(3,5)") + "-่กจ", cols, data }
  }
}

then this api will give anything returned by fn as response body