0

I am building a fake back end server for my Angular2 application and I want to use for this purpose "angular2-in-memory-web-api". I have created the service for my http request and also a typescript file with createDb() method

I am following heroes demo of angular documentation and I do not understand what is the meaning of :

 private heroesUrl = 'api/heroes';  // URL to web api . I do not find this file and I know it should be a json file
 constructor(private http: Http) { }
 getHeroes(): Promise<Hero[]> {
return this.http.get(this.heroesUrl)
           .toPromise()
           .then(response => response.json().data as Hero[])
           .catch(this.handleError);
  }

In my application I have created the InMemoryDbService and all datas are inside createDb() method but also I have a json file in my api folder like api/products. Now when I run the application I got the error "Collection not found". If I comment out the code about InMemoryWebApi I am able to retrieve all the products. I could have gone with the example of json file but I need to do also put, post, get(id:number) requests and I am not able to do it with the usage of json file. I am wondering now win the Angular2 demos which can be found on https://angular.io/docs/ts/latest/tutorial/toh-pt6.html# I do not see any file on api/heroes. Does it mean that this path does not exist, or if it exists is it an empty json file? I have tried to delete the json file, also just do it an empty file but still it is not working. Note: The code is programmatically ok

Dea
  • 291
  • 3
  • 6
  • 17
  • I can tell you how to configure it to able to get (all) the products as well as the heroes. But how do you expect it to be able to post and put to your file. It just doesn't have that power. Why don't you just put the products in the createDb? Otherwise you need a real server that can handle post put, get by id request. A json file a not smart enough to do all that – Paul Samsotha Mar 24 '17 at 11:17
  • @Dea, in the in-memory-web-api there is no file with suffix `.json` and there shouldn't be :) Have you tried and set up the in-memory-web-api like that tutorial explains? :) – AT82 Mar 24 '17 at 11:29
  • @peeskillet I have put all my products inside createDb() method. What I want to realize is what is private heroesUrl = 'api/heroes';? Is this folder existing or not? If we do console.log(heroesUrl) will we get all the products which in real are inside createDb method? – Dea Mar 24 '17 at 11:50
  • The name of the property in the object you return from the createDb is the name of the route. e.g. `return { heroes, products }` will give you a the routes `/api/heroes`, and `/api/products`. The `api` is arbitrary. Any single-segment prefix can be used to prefix the collection path. See [this](http://stackoverflow.com/a/40146891/2587435) – Paul Samsotha Mar 24 '17 at 11:54
  • @peeskillet As I understand this is that we can also use as the url like /myUrlToWebApi/products or it has to be /api/products? Is it also possible to create two different fake back end servers for two different modules in a single application? I mean like one createDb will contain the properties which correspond to the Product model and another createDb to another module will contain the properties which belong to Person model? Thanks in advance – Dea Mar 24 '17 at 12:08
  • It can be `myUrlToWebApi`, but it can only be one level deep. You can't do `/blah/blah/products`. Only `/blah/products`. It doesn't matter what client uses as the prefix. You can use `blah` for one request and `api` for another. It is simply disregarded – Paul Samsotha Mar 24 '17 at 12:09
  • @peeskillet Thanks for your comments. I found them very helpful – Dea Mar 24 '17 at 12:41
  • try api-now on github. If you have nodejs, just run $ npx api-now – ngduc Nov 22 '19 at 05:17

0 Answers0