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?
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.
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.
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.
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:
--entry-point
flag. This name will be used in your resulting URL.--stage-bucket <STORAGE_BUCKET>
command is optional, but I've always used it.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.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With