In my Angular2 application the organization of the folders is as below
Project
|
+-- file 1
|
+-- api //folder
| |
| | +-- listOfProducts //folder
| |
+-- products.json //file
+-- src //folder
| |
+ | +-- app //folder
| +-- product //folder
+-- product.service.ts
+-- product-list.component.ts
+-- //other files
product.service.ts is as below:
import {Injectable} from '@angular/core';
import {Http, Response} from '@angular/http';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/catch';
import {IProduct} from './product';
@Injectable()
export class ProductService{
private _productUrl = 'api/listOfProducts/products.json';
// here 404 error
constructor(private _http: Http) {}
getProducts(): Observable<IProduct[]> {
return this._http.get(this._productUrl)
.map((response: Response) => <IProduct[]> response.json())
.do(data=>console.log("All: " + JSON.stringify(data)))
.catch(this.handleError);
}
private handleError(error: Response) {
console.error(error);
return Observable.throw(error.json().error || "Server error");
}
}
Now after I run my application I got the following error
GET http://localhost:4200/api/listOfProducts/products.json 404 (Not Found)
I am aware the problem is that the path directory is typed wrong but I have tried several paths and still could not resolved my problem. The problem is that I have not still the knowledge how the Angular2 read the files. From the root and below or from the file you are and above?