首页 > 解决方案 > 未为 Firebase 云功能启用 Cors

问题描述

我正在使用 cors npm 包来解决 cors 问题,但无济于事。这以前可以工作,但是当我切换到我的开发环境时,我不断收到此错误:

Access to fetch at 'https://carside.firebaseapp.com/sendsmstoconsumer' from origin 'https://carside.web.app' 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. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

这就是我的函数代码的设置方式:

const functions = require('firebase-functions')
const express = require('express')
const app = express()
const accountSid = 'SECRET'
const authToken = 'SECRET'
const client = require('twilio')(accountSid, authToken)
const cors = require('cors')

// Enable cors for everything
// TODO: Re-enable cors at a later date 
// src: https://www.npmjs.com/package/cors
app.use(cors())

app.post('/sendsmsverificationmessage', cors() ,(request, response) => {

    client.verify.services('SECRET')
        .verifications
        .create({to: request.body.phone, channel: 'sms'})
    .then(verification => {
        response.send("Your verification code has been sent to your phone!" )
    });
})

exports.app = functions.https.onRequest(app)

标签: javascriptnode.jsfirebasegoogle-cloud-functionscors

解决方案


你需要:

const cors = require('cors')({origin: true});

也可以看看:


推荐阅读