Firebase云功能:onRequest和onCall之间的区别 [英] Firebase Cloud Functions: Difference between onRequest and onCall

查看:128
本文介绍了Firebase云功能:onRequest和onCall之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

浏览文档时,我遇到了

...您可以直接通过HTTP请求或客户端的调用来调用函数.

(引号中的链接)是有关functions.https.onCall的提及.

但是在教程此处中,使用了另一个功能functions.https.onRequest,所以我应该使用哪一个?为什么?它们之间的区别/相似之处是什么?

But in the tutorial here, another function functions.https.onRequest is used, so which one should I use and why? What is the difference/similarity between them?

functions.https的文档位于此处.

推荐答案

官方文档

The official documentation for those is really helpful, but from the view of an amateur, the described differences were confusing at first.

  • 这两种类型,在部署时均分配有唯一的HTTPS终结点URL,并且可以直接访问.
  • Both types, when deployed, are assigned with a unique HTTPS endpoint URL and can be accessed directly.
  • 可以直接从客户端应用程序调用(这也是主要目的).

  • Can be invoked (and this is also the main purpose) directly from the client app.

functions.httpsCallable('getUser')({uid})
  .then(r => console.log(r.data.email))

  • 它由用户提供的data automagic context实现.

  • It is implemented with user-provided data and automagic context.

    export const getUser = functions.https.onCall((data, context) => {
      if (!context.auth) return {status: 'error', code: 401, message: 'Not signed in'}
      return new Promise((resolve, reject) => {
        // find a user by data.uid and return the result
        resolve(user)
      })
    })
    

  • context自动包含有关请求的元数据例如uidtoken.
  • 输入dataresponse对象会自动(反)序列化.
  • The context automagically contains metadata about the request such as uid and token.
  • Input data and response objects are automatically (de)serialized.
    • Firebase onRequest Docs
    • Serves mostly as an Express API endpoint.
    • It is implemented with express Request and Response objects.

    export const getUser = functions.https.onRequest((req, res) => {
      // verify user from req.headers.authorization etc.
      res.status(401).send('Authentication required.')
      // if authorized
      res.setHeader('Content-Type', 'application/json')
      res.send(JSON.stringify(user))
    })
    

  • 取决于用户提供的授权标头.
  • 您负责输入和响应数据.
  • 在此处了解更多信息新的Firebase Cloud Functions https.onCall触发器是否更好?

    这篇关于Firebase云功能:onRequest和onCall之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆