json文件的Angular 2 http GET返回404

Ben*_*ins 6 angular-universal angular

我正在做一个POC来证明我在Angular Universal的后端和前端之间有通信.我在后端有一个名为heroes.json的JSON文件,我想从前端服务ModelService中检索model.service.ts.

我有这个文件夹结构:

在此输入图像描述

model.service.ts(前端)我想创建一个http请求来获取一个被调用的方法中的一些数据getStuff().

我在model.service.ts中有这个:

// domain/feature service
@Injectable()
export class ModelService {
  private heroesUrl = "http://localhost:4000/src/backend/heroes.json"; // URL to JSON file
   // This is only one example of one Model depending on your domain
  constructor(public api: ApiService, public cacheService: CacheService, private http: Http) {

  }

  public getStuff(): Observable<any[]> {
        return this.http.get(this.heroesUrl)
                    .map(this.extractData)
                    .catch(this.handleError);
  }

    private extractData(res: Response) {
    let body = res.json();
    return body.data || { };
  }

    private handleError (error: Response | any) {
    // In a real world app, we might use a remote logging infrastructure
    let errMsg: string;
    if (error instanceof Response) {
      const body = error.json() || "";
      const err = body.error || JSON.stringify(body);
      errMsg = `${error.status} - ${error.statusText || ""} ${err}`;
    } else {
      errMsg = error.message ? error.message : error.toString();
    }
    console.error(errMsg);
    return Observable.throw(errMsg);
  }

    // domain/feature service
    @Injectable()
    export class ModelService {
      private heroesUrl = "http://localhost:4000/src/backend/heroes.json"; // URL to JSON file
       // This is only one example of one Model depending on your domain
      constructor(public api: ApiService, public cacheService: CacheService, private http: Http) {

      }

      public getStuff(): Observable<any[]> {
            return this.http.get(this.heroesUrl)
                        .map(this.extractData)
                        .catch(this.handleError);
      }

        private extractData(res: Response) {
        let body = res.json();
        return body.data || { };
      }

        private handleError (error: Response | any) {
        // In a real world app, we might use a remote logging infrastructure
        let errMsg: string;
        if (error instanceof Response) {
          const body = error.json() || "";
          const err = body.error || JSON.stringify(body);
          errMsg = `${error.status} - ${error.statusText || ""} ${err}`;
        } else {
          errMsg = error.message ? error.message : error.toString();
        }
        console.error(errMsg);
        return Observable.throw(errMsg);
      }
Run Code Online (Sandbox Code Playgroud)

从前端组件我调用ModelService.getHeroes:

export class HomeComponent {

      public data: any = {};
      constructor(public modelService: ModelService) {
        // we need the data synchronously for the client to set the server response
        // we create another method so we have more control for testing
        this.universalInit();
      }

      public universalInit() {

        this.modelService.getStuff().subscribe((data) => {
          this.data = data;
        });
      }
Run Code Online (Sandbox Code Playgroud)

我收到这个错误:

GET /src/backend/heroes.json 404 3.698 ms - 46
404 -  {"status":404,"message":"No Content"}
EXCEPTION: 404 -  {"status":404,"message":"No Content"}

/private/var/root/vepo/node_modules/rxjs/Subscriber.js:227
            throw err;
            ^
404 -  {"status":404,"message":"No Content"}
[nodemon] app crashed - waiting for file changes before starting...
Run Code Online (Sandbox Code Playgroud)

所以我private heroesUrl = "http://localhost:4000/src/backend/heroes.json"; // URL to JSON file的服务网址是错误的.鉴于文件夹结构,网址是什么?因为实际运行的项目,输出,是在dist:

在此输入图像描述

所以我不知道该放什么ModelService.heroesUrl.应该ModelService.heroesUrl有什么字符串值?

Vit*_*hyn 8

我把我的同一个文件places.json放到"assets"文件夹中,然后设置了网址:

places = this.http.request("http://localhost:4200/assets/places.json")
Run Code Online (Sandbox Code Playgroud)

希望这些信息对某人有用.


Ami*_*mar 4

您必须将 json 文件放入您的 dist 文件夹客户端中,并且必须将 url 更改为http://localhost:4000/dist/heroes.json<-- destination where you are putting your json file in dist directory