Do Google Cloud Platform HTTP Functions Support Route Parameters?

后端 未结 4 1809
野性不改
野性不改 2021-02-20 16:25

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-

<
相关标签:
4条回答
  • 2021-02-20 16:57

    If you don't want to pass data in the body, you can always put it into a query parameter of the url. Like:

    http://yourGoogleProject.cloudfunctions.net/users?userid={userId}&bookId={bookid}
    

    And in the cloud function you simply access the query parameter from the req object, like:

    exports.users = (req, res) => {
      res.status(200).send(`Hello from user:  + $(req.query.userid) your bookId: $(req.query.bookid)`);
      }
    
    0 讨论(0)
  • You could try modofun: https://modofun.js.org

    Which is a router for multiple operations based on the request path, and also supports automatic parameter parsing from the path.

    It parses the URL path into an operation name and a list of parameters which are populated into req.params, same as with Express when using regex. So you could do something like this:

    var modofun = require('modofun')
    
    exports.bookstore = modofun(
      {
        /**
         * /users/:userId/:bookId
         */
        users: (req, res) => {
          var [userId, bookId] = req.params
          // ...
          res.json(data)
        }
      },
      { mode: 'reqres' }
    )
    

    Or, you can also have the parameters expanded into function arguments and work with pure functions, like this:

    var modofun = require('modofun')
    
    exports.bookstore = modofun(
      {
        /**
         * /users/:userId/:bookId
         */
        users: (userId, bookId) => {
          // ...
          return data
        }
      },
      { mode: 'function' }
    )
    

    I made it for a Google Cloud Functions deployment I have in production, mostly because I don't need the large dependency trail from Express, and I wanted something more lightweight.

    But remember that you can also just use an Express together with Google Cloud Functions. Nothing stops you from creating an Express app with the usual route matching rules and then passing that as the export for your GCloud Function.

    Hope that helps.

    0 讨论(0)
  • 2021-02-20 17:01

    First Create a Handler function,

    function bookIdHandler(req,res){
        let bookId= req.params.bookId;
    
        //your code goes here
    }
    

    You can just pass an Express App object to cloud function routes

    const express = require('express');
    
    const app = express();
    
    app.get('users/:userId/:bookId', bookIdHandler);
    //++ any other express endpoints
    
    
    // Expose Express API as a single Cloud Function:
    exports.widgets = functions.https.onRequest(app);
    

    Ref:How its mentioned in firebase docs

    0 讨论(0)
  • 2021-02-20 17:15

    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.
    0 讨论(0)
提交回复
热议问题