DEV Community

Nishu Goel
Nishu Goel

Posted on

Faking a back-end server in Angular

In this blog post, we will see how to work with data without using a real server. This means without having to use a server where the data resides or without having to working with $http services or requesting APIs, we can use the data and perform CRUD operations with that.

Wait, what is CRUD operations? In computer applications, working with data as to creating, reading, updating, and deleting it on the server or wherever you grab the data from, is what is meant by performing CRUD operations. Any Angular application working with data must be able to perform these four basic operations to be able to read and write the data.

https://thepracticaldev.s3.amazonaws.com/i/o969xfqygu4tknhib2up.jpeg

Coming to the major focus point of this blog post, we will see how to use angular in-memory web API to work with data without actually having to access the data from a real server using http get or post option or through some $http service. This means, we can fake a back-end server and use the application for testing purposes, just like we do on a local emulator.

Angular in-memory web API helps us to work with data for testing purposes. It makes an HTTP request and gets the data from a data store where we have all the data available through a remote API instead of going to the real server .To work with it, we need to install it as a dependency inside our application.

To install angular in-memory web API from the CLI, use the command:

npm install angular-in-memory-web-api — save-dev

Let us break this down. In this command, we install the dependency called angular in-memory web api using npm install. Make sure you have npm and nodejs installed in your machine. Next - -save-dev is used to save the dependency that we just installed for the development purpose.

Now that, we have angular in-memory web api up and runnign in our Angular application, we can quickly go and check inside the package.json as to where this dependency has been updated.

https://thepracticaldev.s3.amazonaws.com/i/a3gauuc9vzp68284uwck.png

Here you go! Under the devDependencies in the package.json, you can find your installed dependency with it version mentioned. devDependencies are those dependencies which are used for the development purposes.

Now, we go on to perform CRUD operations using this installed web dependency.

Step one is to create an entity class where we mention the class name and its properties.

https://thepracticaldev.s3.amazonaws.com/i/bejfo34pw5xkxj5t6a08.png

After our entity class is ready, we will implement InMemoryDbService inside a new class as a service. See the next code for this.

https://gist.github.com/NishuGoel/5076630dbf032fc4a5883d687277fa0d

In this, we are overriding the createDb() method and creating a collection of the entity Users which returns its collection in the end. Another thing that we do here is to import the InMemoryDbService. To use all these, we need to register these inside the module as follows:

https://thepracticaldev.s3.amazonaws.com/i/pkm94u09mp16gcw9lpo6.png

After we have registered it inside the module, we are finally ready to perform the CRUD operations using the angular in-memory web api now.

To do that, let us create a service and perform HTTP operations inside that.

https://gist.github.com/NishuGoel/a3a01c5d1ca091ee5105d293ee59ceff

Now, let us try to understand this code. We have create this service AppService. Inside this service, we are accessing the data through the remote API with the help of this line 12 in the above code i.e.

apiurl = 'api/users';
Next step is to inject the service HttpClient but before we do that, some important files need to be imported.

import {HttpClient, HttpHeaders} from ‘@angular/common/http’;
import {Observable, throwError} from ‘rxjs’;
import { tap, catchError } from ‘rxjs/operators’;
The code in line 14 is setting the variable perfop to perform http operations. Now finally we want to read the data which we receive through this remote call.

To do that, we take a method getUsers() like below:

https://gist.github.com/NishuGoel/06024c31888caaaa799fc0da3f595b13

Now, in the AppComponent,

https://gist.github.com/NishuGoel/1f3000aa17f274589a421d44b9c616f1

We are calling the getUsers() method and returning the data to the created variable users. Also, we are calling the getUsers() method inside ngOnInit() which is the life cycle hook.

Let us finally output what we have created on the component html.

https://gist.github.com/NishuGoel/51b9f9df442d20553d46c468282796e0

Here is how we can read the data using the Angular in-memory web api.

https://thepracticaldev.s3.amazonaws.com/i/yraerxqayp49syx8kwfg.png

Top comments (0)