@airquality/mock-server

Node mock server based on Koa.

Usage no npm install needed!

<script type="module">
  import airqualityMockServer from 'https://cdn.skypack.dev/@airquality/mock-server';
</script>

README

mock-server

Node mock server based on Koa.

  • Writing routes configuration.
  • Custom request handlers.
  • Watching file changes, and restarting server.

Install

npm install @airquality/mock-server

Usage

All .js files in cwd(current work directory) will be watched and treated as routes configuration files. Every .js file must be exported an array property routes. Every route object in routes must exist three properties: path, method and handler. The handler function will be injected a request data object. See following:

// HelloWorld.js
module.exports = {
  routes: [
    {
      path: '/',
      method: 'get',
      handler: ({ headers, params, query, body, cookies, ctx }) => {
        // You can get request data by above params.

        // Returns will be sent to client.
        return 'Hello World!'
      },
    },
  ],
}

In your package.json file, add a script to start mock server by use mock-server command.

// package.json
...
script: {
  "mock": "mock-server",
  ...
}
...

Finally, type npm run mock in your work directory.

npm run mock

Options

Set mockServerOptions in package.json file to change default options.

// package.json
...
"mockServerOptions": {
  // Default path is cwd(current work directory)
  "path": "./routes",
  // Default server port is 4000
  "port": 4001
}
...

Simulated Data

You can use faker.js to generate data.

License

MIT.