Skip to content

Instantly share code, notes, and snippets.

@jthegedus
Last active April 20, 2020 04:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jthegedus/d7227e67389f02e25020cfa760c8cf71 to your computer and use it in GitHub Desktop.
Save jthegedus/d7227e67389f02e25020cfa760c8cf71 to your computer and use it in GitHub Desktop.
Express & Cloud Functions for Firebase example
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
};
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