Nefariously Programmed Mecha

    @airquality/mock-server

    0.1.4 • Public • Published

    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.

    Install

    npm i @airquality/mock-server

    DownloadsWeekly Downloads

    7

    Version

    0.1.4

    License

    MIT

    Unpacked Size

    7.31 kB

    Total Files

    7

    Last publish

    Collaborators

    • npm