How quickly and easily run a local server with fake api data (mocks)?
Sometimes you need to develop a frontend part of a project without a ready-made api, knowing only its structure. In this case, using json-schema-faker, you can generate fake data (mocks) and deploy it to your local server.
Firstly u will need to install json-schema-faker package
yarn add json-schema-faker
Then open the package.json file and add scripts with the following
// ...
"scripts": {
// ...
"start-mockapi": "json-server --watch ./mocks/api/db.json --port 3001",
"generate-mock-data": "node ./generateMockData",
}
After installation, you will need to describe the structure in ./mocks/dataSchema.js of future mocks. You can find more information here.
const schema = {
reports: {
type: 'array',
minItems: 5,
maxItems: 10,
items: {
id: {
type: 'integer',
unique: true,
minimum: 1,
maximum: 1000,
},
title: {
enum: ['production', 'azure data', 'azure data 2'],
},
logo: 'https://picsum.photos/200'
},
},
}
module.exports = schema;
Copy paste script for generating mock data from here in ./generateMockData.js and run the following
yarn generate-mock-data && yarn start-mockapi
Discover More Reads
Categories: