Mock a REST API Back-End for Your Angular 10/9 App with JSON-Server and Faker.js

Mock a REST API Back-End for Your Angular 10/9 App with JSON-Server and Faker.js

In this post we will give you information about Mock a REST API Back-End for Your Angular 10/9 App with JSON-Server and Faker.js. Hear we will give you detail about Mock a REST API Back-End for Your Angular 10/9 App with JSON-Server and Faker.jsAnd how to use it also give you demo for it if it is necessary.

In this tutorial, we’ll learn, with a quick example, how to mock a REST API back-end for your Angular 10/9 application which you can consume using Angular 10 HttpClient even before your real back-end is ready.

We’ll use two powerful open source packages, json-server and faker.js and we assume that you have an Angular project ready.

Please note that both json-server and faker.js are framework agnostic i.e you can use them with any JavaScript framework such as Vue.js or React but in this example we’ll use them in the context of an Angular project.

Why Mocking a Back-End for your Angular 10 App?

More often than not, modern web development involves multiple developers working in separate front-end and back-end applications. This approach has many advantages, such as the separation of concerns but also introduces a few challenges such as the difficulties in coordination between the front-end and back-end developers. Here comes the role of tools such as JSON-Server to ease these difficulties. As a front-end developer, JSON-Server is such a great tool that allows you to spin up a REST API server with a fully-working API with zero coding.

Read to build a real REST API example or also this tutorial which implements a TypeScript/Node.js REST API with JWT Auth. But, as far as Angular concerned, there is no difference between consuming a mocked or real REST API.

How to Mock A REST API Back-End?

Let’s now see how to mock a RESTful API back-end using json-server.

Go to a new terminal, navigate to your Angular project and install json-server from npm:

$ cd ~/angular-example$ npm install --save json-server

Next, create a database.json file inside a server folder with the following object:


We need to add data to this file that will be returned from our REST API endpoints. We can use Faker.js for automatically generating big amounts of data that looks realistic.

Head over to your terminal, go to the root of your Angular project, and install Faker.js from npm:

$ cd ..$ npm install faker --save

Now, create a generateData.js file and add the following code:


Next, add the generateData and runServer scripts to the package.json file:

{"name":"angulardemo","version":"0.0.0","scripts":{"ng":"ng","start":"ng serve","build":"ng build","test":"ng test","lint":"ng lint","e2e":"ng e2e","generate":"node ./server/generate.js > ./server/database.json","server":"json-server --watch ./server/database.json"},

Next, go to your terminal and let’s create some data for our REST API:

$ npm run generateData

Finally, run the REST API server using:

$ npm run runServer

Your REST API server will be available from the [http://localhost:3000/](http://localhost:3000/) address.

Angular 10 backend

These are the available API endpoints that we ca consume using Angular HttpClient:

  • GET /products for getting the products,
  • GET /products/<id> for getting a single product by id,
  • POST /products for creating a new product,
  • PUT /products/<id> for updating a product by id,
  • PATCH /products/<id> for partially updating a product by id,
  • DELETE /products/<id> for deleting a product by id.

You can make use of the _page and _limit parameters to retrieve pages of data. In the Link header of the HTTP response you’ll have the first, prev, next and last links.


Thanks to json-server you can boost your productivity when developing your Angular 10 front-end without waiting for the back-end features to be ready. Since json-server is based on Express.js, it’s even possible to add advanced features to your mocked back-end such as authentication and route protection. Check out this tutorial for an example with authentication or also this npm package.

Hope this code and post will helped you for implement Mock a REST API Back-End for Your Angular 10/9 App with JSON-Server and Faker.js. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US