我正在使用 Node js Express 和 Cloud Firebase 函数构建一个简单的 Web 应用程序
我有2个端点, 1.第一点渲染表单(GET请求)并且 2. 提交时获取表单数据的 POST 端点
出于某种原因,Firebase 在表单提交时跳过帖子 URL(第二个端点)中的函数名称,但它在本地 Express 服务器上工作正常
示例:如果表单 acion 属性值为“check”且 firebase 函数名称为“helloWorld”
表单提交网址应为 "<default firebase pefix>/helloWorld/check"
但它发布的网址是 "<default firebase pefix>/check"
。
url 中的 firebase 函数名称被跳过。这给了我在美国中心等位置找不到的功能
我观察到的另一件事是,如果我将斜杠作为操作属性值的前缀,例如“action =”\check”。firebase 会跳过整个基本 url 并从属性值附加端口值
我尝试通过将静态绝对路径(生成后的路径)设置为表单操作属性来解决此问题。
但我想知道这是一个错误还是我错过了一些东西
<form action="check" method="POST"
<label for="uname"><b>Username</b></label>
<input type="text" placeholder="Enter Username" name="uname" required>
<button type="submit">Login</button>
</form>
//action = "/check"这是一起跳过总的基本网址
const functions = require('firebase-functions');
const express = require('express')
const bodyparser = require('body-parser')
const app = express()
app.set('port',6000)
app.use(bodyparser.json())
app.use(bodyparser.urlencoded({extended:true}))
app.set('view engine', 'ejs');
app.get('/',(req,res)=>{
res.render('formfill')
})
// this below end point is supposed to get triggered on form submission.
// and it is working fine on local express server, but not on firebase functions
app.post('/check',(req,res)=>{
res.send(`you said ${req.body.uname}`)
})
exports.helloWorld = functions.https.onRequest(app);
最佳答案
您无法使用 Cloud Functions 运行 Web 服务器或监听端口。所有尝试运行 Express 的代码都将无法工作。当您部署 HTTP 函数时,会为其分配一个 URL,并且您可以使用该 URL 作为请求的端点。
您应该查看 documentation for HTTP triggers更好地理解它是如何工作的。
关于node.js - 使用 html 表单数据进行发布时的 Firebase 函数路由问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55751466/