angular - How to Configure angular application to connect to remote server in production mode
问题描述
I have an angular application which is hosted on a server 'abc-def.com' and the corresponding REST apis' which allows the user to authenticate and use this angular application are on 'cdf-aqb.com'.
Both angular application and my REST apis' are hosted on tomcat on different servers. How can i configure my angular application connect to the server application once both are hosted in production environment.
In my case ,i have created a angularapp.war which is a part of an installer . This war is deployed in tomcat in abc-def.com. Similarly the REST api's code is part of another installer which is deployed on another server on cdf-aqb.com. In this case i won't know beforehand how to use environment.prod.ts. Because the server application can be hosted anywhere.
Should i ask for an input for the server api's host name, before trying to connect to authenticate to the server?
解决方案
您是否考虑过配置您的 environment.ts 和 environment.prod.ts 文件?
我通常做的是以下几点:
在里面environment.ts
我放了本地网址
export const environment = {
production: false,
BASEURL: 'localhost:3000'
};
然后在`environment.prod.ts 我会有:
export const environment = {
production: true,
BASEURL: 'cdf-aqb.com'
};
您将必须更新所有服务以包含 BASEURL,它看起来如下所示:
return this.http.post<TokenResponse>(`${environment.BASEURL}/api/auth/register`, body);
当您使用 ng build --prod angular 将使用environment.prod.ts
希望这会有所帮助
有用的文章:
https://angular.io/guide/build
https://blog.angulartraining.com/how-to-manage-different-environments-with-angular-cli-883c26e99d15
推荐阅读
- python - 'Bijectively' 将函数列表应用于参数列表
- c# - PUN OnPlayerPropertiesChanged 和对象启用
- javascript - Express.js 代码上的 Windows 脚本宿主错误 800A03EA
- xaml - 搜索结果 TextHighlighter 或 TextRange 是否可以绑定到 UWP XAML 中的 DataTemplate?
- apache - ZShmStorage():致命错误:分配信号量失败。没有权限
- airflow - 带有 TypeError 的气流分支错误:“NoneType”对象不可迭代
- ios - 如何在 Firebase 中使用嵌套数据查询OrderedbyChild 和 queryEqualtoValue?
- excel - 如何在特定工作表列上结束 VBA?
- android - 我需要创建 Android 键盘应用程序,但有些类已被弃用
- facebook - 如何在 unity3d webGL 中进行正确的 facebook 支付?