When started for the first time, Mocks Server will create a config file and a scaffold folder named
mocks in your project, containing next files and folders:
This scaffold contains some examples from this documentation that may help you to better understand how
mocks should be defined, how to use
express middlewares, etc.
The name of the mocks folder can be changed using the path option. Read options for further info.
All files inside the
/routes folder will be loaded, including subfolders, so you can organize the files in the way you want. As a suggestion, you can create a different file for each API entity, and a different folder for each API domain. This will help to maintain your routes organized. For example:
Remember that every file inside the
/routes folder must export an array containing Mocks Server routes.
This file contains the definitions of
mocks, and its name can't be changed (you can also use a
.js extension if you want, or even any other extension if you are using Babel). It must export an array of Mocks Server
You can create other files and folders inside the
mocks folder, Mocks Server will simply ignore them. So, for example, if you want to maintain the data you use in your
routes separated from the definition of the
routes, you could create a
data folder, and import it from the route files.
Every time a file in the
mocks folder is changed (including custom files and folders outside the
routes folder), Mocks Server will reload everything automatically.
If any file contains an error, Mocks Server will add an alert, and you will see the error in the interactive CLI:
The alert will be removed automatically when the file containing the error is fixed and saved again:
We strongly recommend to assign very descriptive ids to the "routes", "variants" and "mocks", as they will be used afterwards in the CLI, the REST API, and all other possible Mocks Server interaction tools.
A good pattern for assigning an id to a
route can be
[method]-[entity], as in
For assigning id to mocks, we recommend to maintain a base
mock named as
default. The rest of mocks should extend from it (at least indirectly), and their ids should be a short description of the mock itself, for example:
Sometimes you'll need to change the response of only one specific route. Instead of creating a new mock for that, remember that it is also possible to change the current variant of one route using the
useRouteVariant method, and to restore the original mock routes variants using the
restoreRoutesVariants method (read API for further info).
These methods are also available through the