0

I want to have a separate function for each HTTP method (GET, POST, PATCH....) - for the same URI path, for example:

// express app
...
getUser.get('/api/v1/user/:id', async (req, res) => {
...
updateUser.patch('/api/v1/user/:id', async (req, res) => {
...

exports.getUser = functions.https.onRequest(getUser);
exports.updateUser = functions.https.onRequest(updateUser);

But I don't know how to specify hosting rewrites configuration for such cases.

Is it possible to route different HTTP methods to different functions (in firebase.json file)?

2
  • When you say "route different HTTP methods", are you saying you want a different methods (like GET and PATCH) for the same URI path to different functions in your Firebase Hosting configuration? Commented Jul 10, 2020 at 19:54
  • @Doug Stevenson yes, main description updated. Commented Jul 10, 2020 at 22:02

2 Answers 2

1

According to the documentation, Firebase Hosting doesn't you specify a method for rewriting. You can only provide a URI path.

What you should probably do here is create a single express app that contains all of the methods for the single endpoint, and export that through a single named function. Express will know what to do with the method.

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

4 Comments

yes, but in this case I will lose all monitoring capability, like performance, memory and etc... for each method... =(
I don't think you have another option.
yeh... looks like platform doesn't support that... I'm considering using specific path like '/api/v1/user/:id'/post, '/api/v1/user/:id'/patch...
Sure, but then your API is no longer very rest-like.
0

There's no great way to handle this. If you use Express then every endpoint is under the same endpoint, as far as Google analytics goes.

One way is to do as you suggested, where you have the word 'get', 'post', etc in the URL, but then you can create one additional endpoint that doesn't have the method in the name (no 'get' or 'post' etc) and in that function check the 'request.method' and then request.redirect to the other appropriate function that has the 'get' 'post' etc in it.

So I have these public urls:

www.myWebsite.com/customer/get
www.myWebsite.com/customer/post

For these internal functions

export const cust_get = onRequest((request, response)...
export const cust_post = onRequest((request, response)...

with these URL rewrites

 {
    "source": "/customer/get/**",
    "function": "cust_get"
  },
  {
    "source": "/customer/post",
    "function": "cust_post"
  },

where of course in the cust_get I'll have to parse out the customer ID from the path (it not using Express).

But then I also add an extra function

export const cust = onRequest((request, response)=>{

 if (request.method == 'POST')
    response.redirect('/customer/post');
 else if (request.method == 'GET')
    response.redirect('/customer/get');//you'll need to also pass any params passed in
}

And then add one more rewrite for that...

  {
    "source": "/customer",
    "function": "cust"
  },

It's still ugly since the client has to make two http calls; however, if they don't like it, they can just call the customer_get or customer_post directly.

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.