I'm new to Angular and I started by doing the tutorial: "Tour of heroes". I didn't understand the HTTP part and I hope that somebody could explained to me. At some point they switched the mock-heroes with an InMemoryDataService and I understood that this service will provide me all the heroes. I did not understand, what is the connection between the service and heroesUrl? I don't see a connection between heroesUrl and InMemoryDataService
Asked
Active
Viewed 168 times
0
-
1[have a look here](https://www.npmjs.com/package/angular-in-memory-web-api). Rest they have simply used `HttpClient` the way it is supposed to – boop_the_snoot Dec 16 '19 at 15:55
-
1The `InMemoryDataService` is a mock service that rather than requesting data via a REST API (api/heroes) it just provides the data via an observable creator (`return of(HEROES)`). `heroesUrl` doesn't exist on the mock service `InMemoryDataService` because it's not going out to a (REST API) server to fetch data. In the more realistic version you use HttpClient to call the api endpoint and as this is asyncronous you get an observable of the data. – Andrew Allen Dec 16 '19 at 15:58
1 Answers
2
The InMemoryDataService
is for testing only. It allows you to test HTTP request without setting up a HTTP server with a webservice.
The InMemoryDataService
intercepts HTTP requests in the browser and replies as if the request went to the server.
It only intercepts request to URLs that begin with the "base path", which by default is "api".
In the createDb()
function, you define a dataset for the service. The sample uses a dataset with only one "collection" named "heroes".
When you do a HTTP request to "api/heroes", the service will intercept the request (because it starts with "api") and returns the "heroes" collection.

rveerd
- 3,620
- 1
- 14
- 30