I am new to Angular, I used to code with PHP and some JavaScript. So I read the Tour of Heroes sample code and still don't understand clearly the concept of HttpClient. Suppose I am going to prepare web API server using MongoDB and Node.js. How can I prepare the code to accept the API request from Tour of Heroes example? Any example code on how to do this?
Also in the Tour of Heroes sample, the part in hero.service.ts which do most of the http processes.
export class HeroService {
private heroesUrl = 'api/heroes'; // URL to web api
Where is this 'api/heroes' refer to? I know that "in-memory-data.service.ts" will handle all the http request. But when I see the code inside, it looks too simple. Can you explain on how "in-memory-data.service.ts" provide the data needed by API request. For example, the search hero function:
searchHeroes(term: string): Observable<Hero[]> {
if (!term.trim()) {
// if not search term, return empty hero array.
return of([]);
}
return this.http.get<Hero[]>(`${this.heroesUrl}/?name=${term}`).pipe(
tap(_ => this.log(`found heroes matching "${term}"`)),
catchError(this.handleError<Hero[]>('searchHeroes', []))
);
}
The function uses the 'api/heroes' and provide query term (Hero name) and will return an array of Hero. I just can't see why http.get can look to api/heroes and get the Hero array. Where's the connection between api/heroes and "in-memory-data.service.ts"? Thank you.