1

The HTTP API provided for the cloud functions mixes all HTTP methods into one API call:

functions.https.onRequest((req, res) => { ... });

This requires a switch to separate out the GETs, POSTs, PUTs and DELETEs. Is there a better way similar to the Express API:

app.get()
app.post()
app.put()
app.delete()

This will allow us to implement resource based endpoints more cleanly.

1 Answer 1

10

Cloud Functions for Firebase allows you to provide an Express.js app to implement HTTP APIs (or whatever else you want to do with an Express.js app).

You can see an example of this in this sample code.

The important thing to note is that you can create an express app, configure it with the endpoints you want, and hand it to Cloud Functions:

const express = require('express');
const app = express();
app.get('/hello', (req, res) => {
  res.send(`Hello ${req.user.name}`);
});
exports.app = functions.https.onRequest(app);
Sign up to request clarification or add additional context in comments.

4 Comments

That's perfect! Thanks for your quick response, @doug_stevenson
Super weird that Cloud Functions are just a wrapper around Express, but helpful, thanks.
@corysimmons Express is a very popular framework for building http apps. It makes sense to leverage something like this because it's familiar and bulletproof.
Yeah, I'm not upset with the idea. I do wish they would've sponsored Doug Wilson before he finally burnt out (seeing as how Google's Cloud Functions are probably making millions—largely in part due to Express). In lieu of that, I wish they'd use Koa. :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.