Last active
April 20, 2020 04:04
-
-
Save jthegedus/d7227e67389f02e25020cfa760c8cf71 to your computer and use it in GitHub Desktop.
Express & Cloud Functions for Firebase example
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 admin = require('firebase-admin'); | |
admin.initializeApp(); | |
// Express Servers | |
const {simpleServer, corsServer, cleanPathServer} = require('./server'); | |
// HTTP Cloud Functions | |
const simple = functions.https.onRequest(simpleServer); | |
const cors = functions.https.onRequest(corsServer); | |
const cleanPath = functions.https.onRequest((request, response) => { | |
if (!request.path) { | |
request.url = `/${request.url}`; // Prepend '/' to keep query params if any | |
} | |
return cleanPathServer(request, response); | |
}); | |
module.exports = { | |
simple, | |
cors, | |
cleanPath | |
}; |
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 cors = require('cors'); | |
const express = require('express'); | |
const simpleServer = express(); | |
simpleServer.get('*', (request, response) => { | |
response.send('Hello from Express on Firebase!'); | |
}); | |
const corsServer = express(); | |
corsServer.use(cors({origin: true})); | |
corsServer.get('*', (request, response) => { | |
response.send('Hello from Express on Firebase with CORS!'); | |
}); | |
const cleanPathServer = express(); | |
cleanPathServer.use(cors({origin: true})); | |
cleanPathServer.get('*', (request, response) => { | |
response.send( | |
'Hello from Express on Firebase with CORS! No trailing \'/\' required!' | |
); | |
}); | |
module.exports = { | |
simpleServer, | |
corsServer, | |
cleanPathServer | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment