2

I am working to create a serverless REST API via Firebase Cloud Functions, which seems to work well but the examples and documentation all seem to use a monolithic solution, since they use the Express framework and essentially map the root http request to the Express app, then let it handle the routing. I understand that this is because the Firebase Hosting platform does not have the ability to handle http verbs.

My expectation was that a serverless / FaaS approach would have a function for each endpoint, making for easy updates in future since there's no need to update the whole app, just that single service - i.e. a more functional approach.

What am I missing here? Why is the approach to use a single function to contain an express app? Doesn't this defeat the purpose of a serverless / Cloud Functions approach? And is there any other way of doing this?

5
  • You can certainly make a function for each endpoint, and express is not required. I think this is fairly clear from the documentation. You are just obliged to use Express-like request and response objects, no need to create an express app. firebase.google.com/docs/functions/http-events Commented Nov 4, 2020 at 15:34
  • Thanks @DougStevenson - perhaps my mistake - I read "The event handler for an HTTP function listens for the onRequest() event, which supports routers and apps managed by the Express web framework." to mean that it only supports routers and apps managed by Express. Are there any example apps where the function-per-endpoint approach (i.e. without Express) is taken? Commented Nov 4, 2020 at 15:42
  • 1
    The documentation I linked to shows that example. You just fill in the code in onRequest. There is no need to route anything. Commented Nov 4, 2020 at 15:45
  • OK, I'll do more digging - couldn't immediately see how to distinguish between POST and GET (for example) on the same url, but I'll assume it's there somewhere :-) Commented Nov 4, 2020 at 15:50
  • I now see that it's in req.method, so should be able to use that - thanks again. Commented Nov 4, 2020 at 15:52

1 Answer 1

1

The documentation shows how to create an endpoint without the help of an Express app, router, or middleware:

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

All you have to do is arrange to send a response with res.send(...) or similar.

Sign up to request clarification or add additional context in comments.

Comments

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.