【问题标题】:Apollo Server resetting CORS options [duplicate]Apollo Server 重置 CORS 选项 [重复]
【发布时间】:2021-07-05 10:33:18
【问题描述】:

Apollo 服务器只是忽略我的 CORS 层并应用它自己的选项。

尽管使用选项反映使用以下代码设置 CORS 中间件:

    var corsOptions = {
       origin: true,  //This will just copy the request origin and put it in response
       optionsSuccessStatus: 200, 
       credentials: true, 
    };
    app.use(cors(corsOptions));

    const apolloServer = new ApolloServer({ schema, context , playground: true}); 
    await apolloServer.start();

   //Integrate with express
   apolloServer.applyMiddleware({app, path: '/api/graphql', cors: corsOptions});

Apollo 服务器只是忽略了我的 CORS 层并返回 'Access-Control-Allow-Origin' : "\*",因此我收到了 CORS 错误,因为:

当请求的凭据模式为“包含”时,响应中的“Access-Control-Allow-Origin”标头的值不能是通配符“*”。

【问题讨论】:

  • 如果目标是让其他帖子更易于搜索,您可以建议修改其标题,而不是写一个全新的问题。

标签: node.js express cors apollo-server


【解决方案1】:

要解决此问题,您需要直接在 applyMiddleware 方法中提供 CORS 选项。 像这样:

var corsOptions = {
           origin: true,  //This will just copy the request origin and put it in response
           optionsSuccessStatus: 200, 
           credentials: true, 
        };
apolloServer.applyMiddleware({app, path: '/api/graphql', cors: corsOptions}); //WE ADDED CORS HERE

这里有一个link 讨论堆栈溢出问题。

另外,如果有兴趣,offical documentation 会说以下内容。

【讨论】:

    猜你喜欢
    • 2020-09-03
    • 2017-10-18
    • 2019-06-26
    • 1970-01-01
    • 2019-11-27
    • 2015-09-26
    • 1970-01-01
    • 1970-01-01
    • 2018-08-24
    相关资源
    最近更新 更多