代码之家  ›  专栏  ›  技术社区  ›  Stewart

如何为前端本地的后端服务器提供服务?

  •  0
  • Stewart  · 技术社区  · 6 年前

    我有一个angular应用程序在apache服务器上运行 http://localhost
    我有一个后端flask服务器在端口5200的同一台机器上运行( http://localhost:5200

    我可以从服务器通过前端访问后端,但是当我从另一台机器尝试时,前端显然会将客户机指向他自己的机器,而这台机器没有运行后端。

    import { Injectable } from '@angular/core';
    import { HttpClient } from '@angular/common/http';
    import { BehaviorSubject, Observable, of } from 'rxjs';
    
    @Injectable()
    export class DataService {    
    
      dataUrl = 'http://localhost:5002/api'
    
      constructor(private http: HttpClient) { }
    
      getData(): Observable<DataItem> {    
        return this.http.get<DataItem>(this.dataUrl)
      }
    }
    

    我有办法做这种事吗?

    dataUrl = 'http://${SameIpAsFrontEnd}:5002/api'
    
    2 回复  |  直到 6 年前
        1
  •  2
  •   kedenk    6 年前

    localhost . You can use the standard development and prod environment file or you can create your own ones. If you use ng serve`它将自动在dev模式下运行,这将提供dev环境文件。但也可以提供任意文件(例如):

    ng serve -e prod
    

    环境文件位于目录中 environments

    export const environment = {
        production: false,
        server: "localhost"
    };
    

    您可以使用 environment.server 例如在你的休息服务中。 在标准角度项目中,将有 environment.prod.ts environment.ts ng serve 没有 -e 将采取 . 如果你使用 -e prod environment.stub.ts ( ng serve -e stub ).

    dataUrl = 'http://' + environment.server + ':5002/api'
    

    要使用特定的env文件构建应用程序,请使用:

    ng build --prod --env=prod
    
        2
  •  0
  •   PR7    6 年前

    这也许会有帮助,

    尝试使用以下命令在第一台计算机上托管站点: ng serve --host 192.168.1.xx --port 80 192.168.1.xx:80