首页 > 解决方案 > 如何解决来自后端层的 Angular 504 错误响应

问题描述

我的本地主机(http)上有角度应用程序,本地主机(https)上有一个弹簧启动应用程序。代理已配置为 Angular 以访问 Spring Boot API,但在从 Angular 调用后端 api 时出现 504 错误。我还尝试使用 proxy.conf.json 创建后端层的代理,但没有奏效。感谢你的帮助

 proxy-config.js-->

    const fs = require("fs");
    const PROXY_CONFIG = {
      "/api/*": {
        changeOrigin: true,
        secure: false,
        target: {
          host: "127.0.0.1:8080",
          passphrase: 'xxxx',
          pfx: fs.readFileSync('./applicationcert.pfx'),
          protocol: "https:"
        }
      }
    };
    module.exports = PROXY_CONFIG;


Sercice.js-->
 this.httpClient.request('GET', '/api/v1', options).subscribe(data => {
    console.log(data)
  },error => {
    console.log(error);
},
() => {
    console.log("Subscribed Else");
});

package.json--> 
    "start": "ng serve --host 127.0.0.1 --proxy-config proxy-config.js",

标签: javascriptangularjsangular

解决方案


在我的角度项目中,我以这种方式集成了 proxy.conf:

在 angular.json 上


"serve": {
  ....
  "options": {
    "proxyConfig": "src/proxy.conf.json",
   },

和我的 proxy.conf.json

{
  "/path/to/api/*": { // with * i can intercept all the api that start wit this url
    "target": "http://localhost:8881", //this is the target where  angular redirect api calls
    "secure": false,
    "logLevel": "debug"
  },

在这里您可以找到更多信息


推荐阅读