Firebase Cloud函数如何处理HTTP发布方法? [英] How Firebase Cloud functions handle HTTP post method?
问题描述
我已经创建了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等... Google完全支持,并且推荐的实现这些类型功能的方式.
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发布方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!