Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do Google Cloud Platform HTTP Functions Support Route Parameters?

This is a bit simpler a question than I tend to like to come here with but I've been driving myself up the wall trying to find an answer to this and I absolutely cannot-

Do Google Cloud Platform HTTP Functions support Route Parameters, as here? http://expressjs.com/en/guide/routing.html#route-parameters

Specifically, I see that Google Cloud Platform HTTP Functions appear to use Express as a base, yet all functions I see any example of already just run off of req and res parameters and nothing else. I can access data within the body, but that doesn't allow me to pull parameters from the route like finding the book ID in a request passed to "/users/:userId/books/:bookId". I can't see how they could be populated into req.params without the ability to specify which part of the path corresponds to which name as here.

I understand that I can always pass them in another way, but this is cleaner and more in keeping with the setup we'd like to use, so I'd really like to make it work if possible. Is there some way to do this that I'm completely missing or is this not supported at all?

like image 944
Amber B. Avatar asked Feb 09 '17 19:02

Amber B.


People also ask

What are routes in GCP?

Google Cloud routes define the paths that network traffic takes from a virtual machine (VM) instance to other destinations. These destinations can be inside your Google Cloud Virtual Private Cloud (VPC) network (for example, in another VM) or outside it.

Does Google Cloud functions use Express?

All requests to HTTPS triggered Cloud Functions are automatically parsed by Cloud Functions with an Express Body Parser before the request reaches your code in the function. So even if you define your parser and middleware through Express, it's still already automatically parsed.

What are HTTP functions?

The primary function of HTTP is to establish a connection with the server and send HTML pages back to the user's browser. It is also used to download data from the server either to the browser or to any requesting application that uses HTTP.


1 Answers

I was able to reach out to the support group for this and it appears that yes, this is supported - you just have to use req.path in order to pull the full path and then parse it in some way (I used path-to-regexp)

Sample code:

exports.myFunction = function(req, res) {
    var keys = [];
    var re = pathToRegexp('/:paramA/:paramB/not-a-param/:paramC/also-not-a-param/:paramD?', keys, {strict: false});
    var pathVars = re.exec(req.path);
    if (pathVars) {
        console.log(JSON.stringify(pathVars));
        var paramA = pathVars[1];
        var paramB = pathVars[2];
        var paramC = pathVars[3];
        var paramD = pathVars[4];
        // Do stuff with the rest of your functionality here
        res.status(200).send('Whatever you wanna send');
    }
}

The command line code to deploy this would then look something like gcloud beta functions deploy myFunction --stage-bucket<STORAGE_BUCKET> --trigger-http (Full documentation for this command here). Your new endpoint URL will then be https://<YOUR_REGION>-<YOUR_PROJECT_ID>.cloudfunctions.net/myFunction, and you can then append subsequent query or route parameters to it when actually making your call (e.g., making a get call to https://<YOUR_REGION>-<YOUR_PROJECT_ID>.cloudfunctions.net/myFunction/paramA/paramB/not-a-param/paramC/also-not-a-param/paramD).

Please note that:

  1. Your function should be exported under the same name as used in the CLI unless you use the --entry-point flag. This name will be used in your resulting URL.
  2. The --stage-bucket <STORAGE_BUCKET> command is optional, but I've always used it.
  3. Cloud Functions will automatically look to a file named index.js or function.js to find your function, but if you provide a package.json file which contains a main entry, then Cloud Functions will look for it instead.
  4. This will, I assume, leave beta at some point, at which you should update to the new command tools.
like image 90
Amber B. Avatar answered Nov 07 '22 05:11

Amber B.