【问题标题】:How to add Cors to PostGraphile如何将 Cors 添加到 PostGraphile
【发布时间】:2021-10-14 15:17:38
【问题描述】:

我正在尝试将 cors 添加到快递服务器中的 PostGraphile 路由中。

app.use(
  postgraphile(process.env.DATABASE_URL || process.env.POSTGRES, ‘public’, {
  watchPg: true,
  graphiql: true,
  enhanceGraphiql: true,
  enableCors: true,
  }),
);

当我尝试从邮递员那里呼叫路线时,我在 Access-Control-Allow-Origin 中得到 *(通配符)。

如何添加特定路线以通过 Access-Control-Allow-Origin 返回?

【问题讨论】:

    标签: node.js express cors postgraphile


    【解决方案1】:

    您可以在 postgraphile 之前使用 cors 中间件并在选项中设置原点:

    const cors = require('cors');
    const { postgraphile } = require('postgraphile');
    
    const options = {
        origin: 'https://your_origin',
    };
    
    app.use(cors(options));
    
    // Enable pre-flight requests for all routes
    app.options('*', cors(options));
    
    app.use(
        postgraphile(process.env.DATABASE_URL || process.env.POSTGRES, ‘public’, {
        watchPg: true,
        graphiql: true,
        enhanceGraphiql: true,
        }),
    );
    

    这会将Access-Control-Allow-Origin 标头设置为https://your_origin

    【讨论】:

      猜你喜欢
      • 2016-04-18
      • 2021-03-22
      • 1970-01-01
      • 2020-01-01
      • 2022-06-21
      • 2019-03-15
      • 2012-09-13
      • 2020-03-16
      • 2017-04-16
      相关资源
      最近更新 更多