【问题标题】:How to fix CORS Node.js on Google App Engine如何在 Google App Engine 上修复 CORS Node.js
【发布时间】:2020-01-17 09:48:58
【问题描述】:

我在 Google App Engine 上部署了 2 个应用程序;

A 是一个 Angular 8 应用程序。

B 是一个 Node.js express 应用程序。

每当我尝试在后端调用 API 时,都会收到此错误:

Access to XMLHttpRequest at '"APPLICATION B"/getGroups?userKey=' from origin 'APPLICATION A' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

我确保在 APPLICATION B 的谷歌应用引擎凭据中启用 APPLICATION A url 作为授权 Javascript 源和授权重定向 URI。 在 localhost 中一切正常。

我在 App.js 上的尝试:

尝试 1

app.use(function (req, res, next) {
res.header(“Access-Control-Allow-Origin”, “*”);
res.header(“Access-Control-Allow-Methods”, “GET,HEAD,OPTIONS,POST,PUT”);
res.header(“Access-Control-Allow-Headers”, “Origin, X-Requested-With, Content-Type, Accept, x-client-key, x-client-token, x-client-secret, Authorization”);
next();
});

尝试 2

var cors = require('cors')
app.use(cors())

尝试 3

var cors = require('cors')

var corsOptions = {
  origin: 'APPLICATION A',
}

app.use(cors(corsOptions ));
app.options('*', cors());

这是我在 Angular 8 应用程序 A 中执行的 GET

  getGroupsRequest(id): Observable<any> {
    const url = "APPLICATION B"
    return this.http.get(url + id);
  }

如果我使用 CORS 插件或打开谷歌浏览器“--disable-web-security”,它就可以工作。

"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --user-data-dir="C:\Users\510919\ChromeDev" --disable-web-security --auto-open-devtools-for-tabs

【问题讨论】:

    标签: node.js angular google-app-engine cors


    【解决方案1】:

    在你的 Node.js 文件中的 app.js 中试试这个

    var express = require('express')
      ,cors = require('cors')
      , app = express();
    
    var originsWhitelist = [
      '<FRONT-END URL>',     
       'http://www.myproductionurl.com'
    ];
    var corsOptions = {
      origin: function(origin, callback){
            var isWhitelisted = originsWhitelist.indexOf(origin) !== -1;
            callback(null, isWhitelisted);
      },
      credentials:true
    }
    
    app.use(cors(corsOptions));
    

    这将在您的 Node.js 上启用

    现在你需要准备你的 Angular 应用来支持 CORS

    要启用 CORS,您可以扩展 BrowserXhr 并将其包含在引导过程中。在您的 Angular 应用程序项目中创建一个名为 cust-ext-browser-xhr.ts 的文件并粘贴以下代码:

    import {Injectable} from "@angular/core";
    import {BrowserXhr} from "@angular/http";
    @Injectable()
    
    export class CustExtBrowserXhr extends BrowserXhr {
      constructor() {
          super();
      }
      build(): any {
        let xhr = super.build();
        xhr.withCredentials = true; 
        return <any>(xhr);
      }
    }
    

    为了使用您的 Custom BrowserXhr,您需要执行以下操作。

    import { provide } from '@angular/core';
    import { AppComponent} from './app/';
    import { BrowserXhr } from '@angular/http';
    import {CustExtBrowserXhr} from './app/path-to-file/cust-ext-browser-xhr';
    @NgModule({
      imports: [
        HttpModule,
        BrowserModule,
        …
      ],
      declarations: [AppComponent],
      providers: [
        {provide: BrowserXhr, useClass:CustExtBrowserXhr},
        {provide: LocationStrategy, useClass: HashLocationStrategy}
        …
      ],
      bootstrap: [ AppComponent ]
    })
    

    希望对你有帮助。

    【讨论】:

    • 验证您正在使用您的自定义浏览器Xhr,并验证您的所有网址,也许您有错字或其他什么
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-13
    • 1970-01-01
    • 2014-06-22
    • 2021-01-29
    • 2018-02-06
    相关资源
    最近更新 更多