【问题标题】:Angular takes wrong url for api after move to production转移到生产环境后,Angular 为 api 提供了错误的 URL
【发布时间】:2019-10-11 07:23:20
【问题描述】:

我是 Angular 新手,对移动生产知之甚少。我在本地更改了一些代码,它正在工作。

当我应用命令ng build --prod 时,它将在节点项目public 文件夹中生成生产块文件。我在实时服务器(Godaddy 共享主机)上移动了这些文件,然后网站运行正常。

但是当尝试使用 api 与服务器通信(如登录、注册)时,它将使用我的本地主机 URL http://localhost:8080 而不是我的域 URL。

如果我恢复到旧代码,这个项目在实时服务器中运行良好。

我的angular.json 文件

"build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "../server/serverAPI/public",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.app.json",
            "assets": [
              "src/favicon.png",
              "src/assets",
              "src/manifest.json",
              "src/manifest.json"
            ],
            "styles": [
              "src/styles.css",
              "./node_modules/bootstrap/dist/css/bootstrap.min.css",
              "./node_modules/jquery/dist/jquery.min.js",
              "./node_modules/bootstrap/dist/js/bootstrap.min.js"
            ],
            "scripts": ["node_modules/moment/min/moment.min.js"]
          },
          "configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "aot": true,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true,
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "4mb",
                  "maximumError": "5mb"
                }
              ],
              "serviceWorker": true
            }
          }
        },

environment.ts

export const environment = {
  production: false
};

environment.prod.ts

export const environment = {
  production: true,
  baseUrl: 'https://example.com'
};

【问题讨论】:

  • 你能显示你是如何存储你的 api url 可能是你将它存储在 environment.ts 文件中
  • 它纯粹是你的环境文件配置..
  • 我在 environment.prod.ts 文件中将 API URL 存储为 'baseUrl'
  • 请检查您的 environment.prod.ts 文件。
  • 你是否在你的angular.json配置文件中进行了相应的配置????

标签: node.js angular proxy url-rewriting


【解决方案1】:

看起来您将配置存储在 environment 中,并且不要在生产配置中添加 fileReplacements,因此请检查您的 angular.json 生产配置 fileReplacements 它应该是

"production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],

【讨论】:

  • 如果是这样,请检查您的 api url 是否没有硬编码或共享一些代码,您如何调用您的 api 和 angular.json
【解决方案2】:

尝试ng build --configuration=production,因为这是您的 Angular json 定义为“生产配置”的内容。

【讨论】:

  • 我试过这种方法,但没有用。我也试过ng build --prod --configuration=production它也不起作用
  • 你试过这个确切的语法吗?我注意到上面那些在配置和生产之间缺少= 符号的地方。
  • 是的,我尝试过与= 完全相同的语法,也尝试过不使用= 两者都不起作用
猜你喜欢
  • 1970-01-01
  • 2018-08-21
  • 2015-03-21
  • 1970-01-01
  • 2019-11-02
  • 2012-11-23
  • 1970-01-01
  • 2018-06-04
  • 1970-01-01
相关资源
最近更新 更多