Do Google Cloud Platform HTTP Functions Support Route Parameters?

后端 未结 4 1827
野性不改
野性不改 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:59

    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.

提交回复
热议问题