Data Mocking
Modern.js allows you to easily generate mock data so that the front-end can develop independently without depending on the back-end API.
Enabling Mock
By convention, when there is an index.ts in the config/mock/ directory, mock data will be automatically enabled:
Writing Mock Files
The config/mock/index.ts file only needs to export an object containing all Mock APIs. The properties of the object are composed of the request configuration method and url, and the corresponding property values can be Object, Array, or Function:
When you access http://localhost:8080/api/getInfo, the API will return JSON { "data": [1, 2, 3, 4] }.
Return Random Data
Libraries such as Mock.js can be used in config/mock/index.js to generate random data. For example:
Delayed Response
- You can do this by using the browser's "weak connection simulation" feature.
- Delays can be set via
setTimeout, for example:
Use Mock On Demand
Under the config/mock/index.ts, you can also export the config to control the Mock service.
Currently only the enable configuration is supported, which allows developers to control whether to execute mock.
After modifying config, there is no need to restart the service, which will take effect immediately.