【问题标题】:cors not working in firebase functions with expresscors 不能在具有 express 的 firebase 功能中工作
【发布时间】:2021-05-24 13:44:09
【问题描述】:

我在使用 express 的 firebase 函数中遇到 cors 问题,我已经尝试了很多关于这个特定问题的方法,这些只是其中的一部分:

我尝试在res.send()之前设置标题:

res.setHeader("Access-Control-Allow-Origin", '*');

我试图让请求通过 cors 中间件:

return cors(req, res, async() => { await ... } )

尝试将 cors 原点设置为 "Origin": true"Origin": "*"

例如尝试设置app.options()

app.options('*', cors()) // for every route

这些都不起作用,我得到了预检错误,正如我所说,我从博客文章和这里 (stackoverflow) 尝试了很多东西,没有一个答案真的对此有帮助,所以有人可以解释我为什么会得到下面的错误,那是什么预检,为什么它没有通过控制检查?

访问 XMLHttpRequest 在 'https://us-central1-projectName.cloudfunctions.net/api/create-customer' 来自原点“http://localhost:4200”已被 CORS 策略阻止: 对预检请求的响应未通过访问控制检查:否 请求中存在“Access-Control-Allow-Origin”标头 资源。

我在后端方面的经验很少,我认为现在尝试弄清楚这一点肯定会对将来有所帮助,因为我仍在学习后端。

想看完整代码的朋友:

import * as functions from 'firebase-functions';
import * as express from 'express';
import Stripe from 'stripe';
import cors from 'cors';

const app = express();

app.use(cors({ origin: true }));
app.options('*', cors({ origin: true }));

const stripe = new Stripe(
  'API KEY',
);

app.get('/process-subscription', async (req, res) => {
  const endReq = () => {
    res.end();
  };
  const {
    customerId,
    checkPlan,
  } = req.params;

  if (checkPlan !== 'premium') {

    try {
      console.log(customerId);
      const subscription = await stripe.subscriptions.create({
        customer: customerId,
        items: [{ price: 'id' }], 
      });


      res.setHeader('Access-Control-Allow-Origin', '*');
      return res.send(subscription.id);
    } catch (err) {
      res.send(err);
      endReq();
    }
  } else {
    return res.send({
      isPremium: true,
      message: 'You already own a Premium Subscription!',
    });
    endReq();
  }
});

app.get('/create-customer', async (req, res) => {
  return cors(req, res, async () => {
    const endReq = () => {
      res.end();
    };
    const { email, name } = req.body;

    try {
      const customer = await stripe.customers.create({
        email: email,
        name: name,
      });
      res.setHeader('Access-Control-Allow-Origin', '*');
      return res.status(200).send(customer.id);
    } catch (err) {
      res.send(err);
      endReq();
    }
  });
});

app.get('/attach-method', async (req, res) => {
  res.set('Access-Control-Allow-Origin', '*');
  const endReq = () => {
    res.end();
  };

  const { customerId, paymentMethod } = req.params;
  try {
    const attachMethod = await stripe.paymentMethods.attach(paymentMethod, {
      customer: customerId,
    });
    let customer = await stripe.customers.update(customerId, {
      invoice_settings: {
        default_payment_method: paymentMethod,
      },
    });
    res.setHeader('Access-Control-Allow-Origin', '*');
    return res.status(200).send('Success');
  } catch (err) {
    res.send(err);
    endReq();
  }
});

export const api = functions.https.onRequest(app);

谢谢。

【问题讨论】:

标签: node.js express google-cloud-functions cors


【解决方案1】:

解决这个问题,而不是在快速路线中使用

return cors(req, res, () => {...})

我用过这个,只是在cors后面加了括号:

return cors()(req, res, () => {...})

【讨论】:

  • 你能分享你的整个代码吗?
猜你喜欢
  • 2018-05-15
  • 2021-01-29
  • 2021-03-05
  • 2020-12-01
  • 2019-12-11
  • 1970-01-01
  • 2021-10-01
  • 2018-08-27
  • 1970-01-01
相关资源
最近更新 更多