Skip to content

Instantly share code, notes, and snippets.

@mediavrog
Last active April 14, 2023 08:56
Show Gist options
  • Star 33 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save mediavrog/49c4f809dffea4e00738a7f5e3bbfa59 to your computer and use it in GitHub Desktop.
Save mediavrog/49c4f809dffea4e00738a7f5e3bbfa59 to your computer and use it in GitHub Desktop.
CORS in Google Cloud Functions for Firebase
const cors = require('cors')({origin: true});
exports.sample = functions.https.onRequest((req, res) => {
cors(req, res, () => {
res.send('Passed.');
});
});
@mikemanders
Copy link

@jesster2k10 Your answer seems great, but passing the regions with the spread operator gives an typescript error.
Argument of type 'string' is not assignable to parameter of type '"europe-west2" | "europe-west3" | "us-central1" | "us-east1" | "us-east4" | "europe-west1" | "asia-east2" | "asia-northeast1"'.ts(2345).
Do you know how to fix this? For now I'm sticking to one region to overcome this problem.

Thanks for the great answer.

@jimbits
Copy link

jimbits commented Dec 21, 2022

@jpfong I want to thank you for your post I have been spending the last 4 hours trying to get cors working. So random why would they not put an example in the docs. Thank you so much

@rgbskills
Copy link

Hello, Im getting the cors error and I can figure out why, here is may code:

import express from "express";
import cors from "cors";

const expressApp = express();
expressApp.use(cors({origin: true}));

expressApp.get("/Hello", (req, res) => {
  res.send("Hello!");
});

Can anyone help me out?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment