DEV Community

Maxim Romanov for JetRockets

Posted on • Edited on • Originally published at jetrockets.pro

15 8

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

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

nextjs tutorial video

Youtube Tutorial Series 📺

So you built a Next.js app, but you need a clear view of the entire operation flow to be able to identify performance bottlenecks before you launch. But how do you get started? Get the essentials on tracing for Next.js from @nikolovlazar in this video series 👀

Watch the Youtube series