Create Mock API'
npm install mock-api-middlewarenpm install --save-dev mock-api-middleware
mam(route, [options])
javascript
const mam = require('mock-api-middleware');
// You could put this directly in the middleware array of browserSync
const mockApi = mam('/mockapi', { // <--- Route where to mount the API
mockPath: './mocks/' // <--- Where to find the API files
});
browserSync.init({
server: './dist',
middleware: [
mockApi // <--- Just put it here like anyother middleware
]
});
`
Options
This module accepts the following options
- mockPath: Path to mock files
- dataset: Override datasets with this, accepts an object. Will only override if the sames keys are defined.
- helpers: Your own helpers which are described in "writing-your-own-helpers" of dummy-json
Mocking
To mock an API or a service. All you need is a folder and some files.
`
mocks
│ users.GET.json
└───users
│ │ _.GET.json
│ │ admin.GET.json
│ │ ...
│
│ products.GET.json
└───products
│ theonlyproduct.GET.json
`
The latter folder example would create an API with endpoints like this:
/mockapi/users
/mockapi/users/admin
/mockapi/users/*
/mockapi/products
/mockapi/products/theonlyproduct
The only magic thing about this is the _.GET.json` this is a CATCH-ALL that will be a fallback for all "missing files" in the current folder.
| Template string | Output string |
|
|