Last active
March 4, 2019 06:32
-
-
Save jthegedus/b02f783febbf57cd1e66f817f331663c to your computer and use it in GitHub Desktop.
Cloud Functions for Firebase - Express example 3/3 - using CORS and automatically appending a trailing slash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const functions = require("firebase-functions") | |
const cors = require("cors") | |
const express = require("express") | |
/* Express with CORS & automatic trailing '/' solution */ | |
const app3 = express() | |
app3.use(cors({ origin: true })) | |
app3.get("*", (request, response) => { | |
response.send( | |
"Hello from Express on Firebase with CORS! No trailing '/' required!" | |
) | |
}) | |
// not as clean, but a better endpoint to consume | |
const api3 = functions.https.onRequest((request, response) => { | |
if (!request.path) { | |
request.url = `/${request.url}` // prepend '/' to keep query params if any | |
} | |
return app3(request, response) | |
}) | |
module.exports = { | |
api3 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nice