Firebase Cloud 函数如何处理 HTTP post 方法? [英] How Firebase Cloud functions handle HTTP post method?

查看:26
本文介绍了Firebase Cloud 函数如何处理 HTTP post 方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了 Firebase Cloud Functions 应用,我用 https.onRequest 创建了函数.并使用 req.body 获取数据,但那里没有数据.

I have created Firebase Cloud Functions app, I created function with https.onRequest. and get data with req.body but there is not data there.

Firebase Cloud Functions 可以处理 HTTP POST 方法吗?

Can Firebase Cloud Functions can handle HTTP POST method?

这是我的示例代码:-

var functions = require('firebase-functions');

exports.testPost = functions.https.onRequest((req, res) => {
    console.log(req.body);
});

我通过邮递员使用 POST 方法进行了测试,但没有在 Firebase 日志中显示结果.

I tested by postman with POST method but didn't show result in Firebase log.

推荐答案

基于 Firebase 构建的功能也可以使用 Express.js 路由器来处理 GET/POST/PUT/DELETE 等...谷歌完全支持,并且是实现这些类型功能的推荐方法.

Functions built on Firebase can also use Express.js routers for handling GET/POST/PUT/DELETE, etc... is fully supported by Google, and is the recommended way to implement these types of functions.

更多文档可以在这里找到:

More documentation can be found here:

https://firebase.google.com/docs/functions/http-events

这是一个基于 Node.js 构建的工作示例

Here's a working example built on Node.js

const functions = require('firebase-functions');
const express = require('express');
const cors = require('cors');
const app = express();

// Automatically allow cross-origin requests
app.use(cors({ origin: true }));

app.get('/hello', (req, res) => {
  res.end("Received GET request!");  
});

app.post('/hello', (req, res) => {
  res.end("Received POST request!");  
});

// Expose Express API as a single Cloud Function:
exports.widgets = functions.https.onRequest(app);

然后,运行 firebase deploy,它应该会编译您的代码并创建新的小部件"功能.注意:您可以将小部件重命名为任何您想要的名称.最终,它将生成一个用于调用该函数的 URL.

Then, run firebase deploy, and that should compile your code and create the new "widgets" function. Note: You can rename widgets to anything you want. Ultimately, it will generate a URL for calling the function.

这篇关于Firebase Cloud 函数如何处理 HTTP post 方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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