【发布时间】:2021-03-17 15:48:56
【问题描述】:
我使用 Firebase Functions 创建了一个 Express 应用,并将文件托管在 Firebase Hosting 上。
index.js(Firebase 函数):
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const express = require('express');
const parse = require('body-parser');
const cors = require('cors');
admin.initializeApp();
const app = express();
app.use(cors({ origin: true }));
app.use(parse.json());
app.use(parse.urlencoded({ extended: false }));
app.post('/create-url', (req, res) => {
console.log(req.body);
res.send(req.body);
});
exports.app = functions.https.onRequest(app);
firebase.json:
{
"hosting": {
"public": "public",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "**",
"function": "app"
}
]
},
"functions": {
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run lint"
]
},
"emulators": {
"functions": {
"port": 5001
},
"firestore": {
"port": 8080
},
"hosting": {
"port": 5000
},
"ui": {
"enabled": true,
"port": 4000
}
}
}
当我向 http://localhost:5000/create-url 发出 HTTP 请求时,req.body 是 emtpy,但是当我向 http://localhost:5001/PROJECT_NAME_HERE/us-central1/app/create-url 发出请求时(我已经隐藏了项目名称),它可以正常工作。我正在使用 Postman 发出请求,但它也不适用于我的前端代码:(。如果我部署到 Hosting and Functions 并通过 web.app 域访问应用程序,它就可以工作。我在running firebase emulators:start 在本地运行。
提前感谢您的帮助!
【问题讨论】:
标签: node.js firebase express google-cloud-functions firebase-hosting