Skip to content

Instantly share code, notes, and snippets.

@FazioNico
Created June 28, 2022 18:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FazioNico/15d797a46e9196f5f043ae54a8f40c6f to your computer and use it in GitHub Desktop.
Save FazioNico/15d797a46e9196f5f043ae54a8f40c6f to your computer and use it in GitHub Desktop.
import * as functions from "firebase-functions";
import { Request, Response } from "firebase-functions";
import * as cors from 'cors';
const corsHandler = cors({origin: true});
// Start writing Firebase Functions
// https://firebase.google.com/docs/functions/typescript
export const helloWorld = functions.https.onRequest((request: Request, response: Response) => {
corsHandler(request, response, () => {
functions.logger.info("Hello logs!", {structuredData: true});
// browsers like chrome need these headers to be present in response if the api is called from other than its base domain
response.set("Access-Control-Allow-Origin", "*"); // you can also whitelist a specific domain like "http://127.0.0.1:4000"
response.set("Access-Control-Allow-Headers", "Content-Type");
response.json({"message": "Hello from Firebase!"});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment