Help us understand the problem. What is going on with this article?

Cloud Functions for Firebase でCORSを許可する方法

More than 1 year has passed since last update.

割とCORSを使用したいシチュエーションは多いのではないかと思い共有。
そもそものCloud Functionsの使い方が分からない方はこちらを参照してみてください。

expressのcorsパッケージを使うことによって実現します。

上記パッケージを npm install cors もしくは yarn add cors でインストールして、CORSを許可したい関数を以下のようにwrapします。

index.js
const functions = require('firebase-functions');
//importする際に {origin: true} をつける
//もう少し詳細に指定したい場合はこちらをご参照ください https://github.com/expressjs/cors#configuration-options
const cors = require('cors')({origin: true});

exports.withcors = functions.https.onRequest((request, response) => {
  //関数内全体をwrap
 cors(request, response, () => {
    response.status(200).send('success!');
  });
});

すごい短くなってしまいましたが、ご覧の通り簡単に実現できるので困ったらお試しください。

seya
最近の趣味はGraphQLとFigmaです。
https://note.mu/seyanote
linc-well
Linc'well(リンクウェル)は2018年創業のヘルスケアスタートアップです。我々は、医療のIT化を通じて、人々と社会の健康に貢献します。
https://www.linc-well.com/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした