【问题标题】:Angular 11 And Zuul Gateway develop problem with hot reloadingAngular 11 和 Zuul Gateway 出现热重载问题
【发布时间】:2021-04-25 06:40:27
【问题描述】:

我正在使用 Angular 11 开发 Angular 应用程序。应用程序在 http://localhost:4200 中以开发模式运行。后端应用程序位于使用 Zuul 2.2 配置的网关后面,其 url 为 https://localhost:8447。当要求登录页面(或任何其他页面)时,我看到一些对wss://localhost:8447 的请求被拒绝,并且浏览器中出现此错误:

WebSocket connection to 'wss://localhost:8447/ui/sockjs-node/197/jxkw5ek0/websocket' failed: Error during WebSocket handshake: Unexpected response code: 400

我想 Angular 正在尝试对 4200 端口中的节点服务器进行热模式重新加载。

我在package.json做了这个修改

"start": "ng serve --base-href /ui/ --public-host http://localhost:4200/sockjs-node"

但是在这种情况下,浏览器中出现了这个错误:

zone-evergreen.js:2845 GET https://localhost:4200/ui/sockjs-node/info?t=1611158080859 net::ERR_SSL_PROTOCOL_ERROR

URL https://localhost:4200 不正确,因为 Angular 部署在非安全端口中

有什么问题吗?这是由于 Webpack 或 Angular 中的错误造成的吗?

package.json
{
  "name": "xxxx",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve --base-href /ui/",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "~11.0.5",
    "@angular/common": "~11.0.5",
    "@angular/compiler": "~11.0.5",
    "@angular/core": "~11.0.5",
    "@angular/forms": "~11.0.5",
    "@angular/platform-browser": "~11.0.5",
    "@angular/platform-browser-dynamic": "~11.0.5",
    "@angular/router": "~11.0.5",
    "rxjs": "~6.6.0",
    "tslib": "^2.0.0",
    "zone.js": "~0.10.2"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.1100.5",
    "@angular/cli": "~11.0.5",
    "@angular/compiler-cli": "~11.0.5",
    "@types/jasmine": "~3.6.0",
    "@types/node": "^12.11.1",
    "codelyzer": "^6.0.0",
    "jasmine-core": "~3.6.0",
    "jasmine-spec-reporter": "~5.0.0",
    "karma": "~5.1.0",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage": "~2.0.3",
    "karma-jasmine": "~4.0.0",
    "karma-jasmine-html-reporter": "^1.5.0",
    "protractor": "~7.0.0",
    "ts-node": "~8.3.0",
    "tslint": "~6.1.0",
    "typescript": "~4.0.2"
  }
}
angular.json

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "otaplatform-ui": {
      "projectType": "application",
      "schematics": {},
      "root": "",
      "sourceRoot": "src",
      "prefix": "app",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/xxxxxxx",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.app.json",
            "aot": true,
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.css"
            ],
            "scripts": []
          },
          "configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "namedChunks": false,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true,
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "2mb",
                  "maximumError": "5mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "6kb",
                  "maximumError": "10kb"
                }
              ]
            }
          }
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "xxxxxxxxxxx:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "xxxxxxxxxxx:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "xxxxxxxxxxx:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.spec.json",
            "karmaConfig": "karma.conf.js",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.css"
            ],
            "scripts": []
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "tsconfig.app.json",
              "tsconfig.spec.json",
              "e2e/tsconfig.json"
            ],
            "exclude": [
              "**/node_modules/**"
            ]
          }
        },
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "xxxxxxxxxxx:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "xxxxxxxxxxx:serve:production"
            }
          }
        }
      }
    }
  },
  "defaultProject": "xxxxxxxxxxx"
}

【问题讨论】:

    标签: angular webpack netflix-zuul


    【解决方案1】:

    最后我决定使用 --ssl 标志

    "start": "ng serve --base-href /ui/ --ssl --ssl-key ssl/localhost.key --ssl-cert ssl/localhost.crt --public-host localhost:4200"
    

    并在 Zuul 中重定向到 https://localhost:4200 来解决问题。

    这不是最好的解决方案,但它确实有效。可能这个问题是Angular 11版本的问题,我没查过

    【讨论】:

      猜你喜欢
      • 2016-06-05
      • 2019-09-14
      • 2022-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-13
      相关资源
      最近更新 更多