@lakca/webpack-mock

1.0.0 • Public • Published

Webpack Dev Server Mock Middleware

Add mocked routes to webpack dev server. Also, this is just an express middleware, you can use this in any express used.

Usage

See detail in test.

const mock = require('@lakca/webpack-mock')

module.exports = {
  devServer: {
    before(app) {
      mock(options)(app)
    }
  }
}
const mock = require('@lakca/webpack-mock')
const app = express()
mock(options)(app)

Options

options.routeFiles {string|string[]}

Specify mock routes definition file/files, regarded as absolute path/paths where mock routes definition files are.

options.watch {string|string[]}

Watch mock folders/files to refresh mock routes. if any folder will effect the mock data, it should be included.

Mock Route Example

See more examples in test, and template structure detail in source code

[
  'get', // method
  '/url-response/:name', // url
  '${req.params.name}' // response
]
[
  'get',
  '/method-url-response-range/:name',
  '${req.params.name}',
  '1..2' // response is an array of response template, array length is within 1 and 2.
],
[
  'get',
  '/method-url-response-args/:name',
  '${req.params.name}${arg0}',
  { arg0: 'hello' } // additional arguments of template interpolation.
],
[
  'get',
  '/method-url-response-process_return/:name',
  '${req.params.name}',
  // additional processor of response data.
  (r/** generated response data */, req, res) => r + req.method
],
[
  'get',
  '/response_is_function/:name',
  // determine response by function instead of template structure.
  (req, res, {count /** exact array length */, render /** render function of template structure */}) => render('${req.params.name}', { req })
],

LICENSE

MIT

Readme

Keywords

Package Sidebar

Install

npm i @lakca/webpack-mock

Weekly Downloads

0

Version

1.0.0

License

MIT

Unpacked Size

15.9 kB

Total Files

8

Last publish

Collaborators

  • lakcaorg