[英]Deploying express server code as firebase cloud function for react app
I have the following express server code which I would like to run as a firebase cloud function.我有以下快速服务器代码,我想将其作为 Firebase 云功能运行。 However I have this error (page not found) POST https://vid-chat-app.web.app/video/token 404
.但是我有这个错误(页面未找到) POST https://vid-chat-app.web.app/video/token 404
。 What am I doing wrong here?我在这里做错了什么?
const config = require('./config');
const express = require('express');
const bodyParser = require('body-parser');
const pino = require('express-pino-logger')();
const { videoToken } = require('./tokens');
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(pino);
const sendTokenResponse = (token, res) => {
res.set('Content-Type', 'application/json');
res.send(
JSON.stringify({
token: token.toJwt()
})
);
};
app.post('/video/token', (req, res) => {
const identity = req.body.identity;
const room = req.body.room;
const token = videoToken(identity, room, config);
sendTokenResponse(token, res);
});
This is my attempt to do convert the code to a cloud function这是我尝试将代码转换为云函数
const functions = require('firebase-functions');
const config = require('./config');
const express = require('express');
const bodyParser = require('body-parser');
const pino = require('express-pino-logger')();
const { videoToken } = require('./tokens');
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(pino);
const sendTokenResponse = (token, res) => {
res.set('Content-Type', 'application/json');
res.send(
JSON.stringify({
token: token.toJwt()
})
);
};
app.post('/video/token', (req, res) => {
const identity = req.body.identity;
const room = req.body.room;
const token = videoToken(identity, room, config);
sendTokenResponse(token, res);
});
exports.app = functions.https.onRequest(app)
Here is the firebase.json file这是 firebase.json 文件
{
"hosting": {
"public": "build",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "/video/token",
"destination": "app"
},
{
"source": "**",
"destination": "/index.html"
}
]
},
"functions": {
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run lint"
],
"source": "functions"
}
}
And here is the api request in the react app这是反应应用程序中的 api 请求
const handleSubmit = useCallback(
async event => {
event.preventDefault();
const data = await fetch('/video/token', {
method: 'POST',
body: JSON.stringify({
identity: username,
room: roomName
}),
headers: {
'Content-Type': 'application/json'
}
}).then(res => res.json());
setToken(data.token);
},
[roomName, username]
);
UPDATE更新
I have edited part of the firebase.json file to我已将 firebase.json 文件的一部分编辑为
{ "source": "/video/token", "function": "app" }, { "source": "/video/token", "function": "app" },
That seems to work.这似乎有效。 I have created a test GET request and tested the address https://vid-chat-app.web.app/video/token on Postman.我创建了一个测试 GET 请求并在 Postman 上测试了地址https://vid-chat-app.web.app/video/token 。 It works!有用! However, I now have the following 500 error但是,我现在有以下 500 错误
Error: accountSid is required
at new AccessToken (/srv/node_modules/twilio/lib/jwt/AccessToken.js:213:28)
at generateToken (/srv/tokens.js:6:10)
at videoToken (/srv/tokens.js:20:17)
at app.get (/srv/index.js:32:17)
at Layer.handle [as handle_request] (/srv/node_modules/express/lib/router/layer.js:95:5)
at next (/srv/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/srv/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/srv/node_modules/express/lib/router/layer.js:95:5)
at /srv/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/srv/node_modules/express/lib/router/index.js:335:12)
Try尝试
app.post('/app', (req, res) => { ... })
since that's what the Cloud Functions URI would be if Hosting hadn't forwarded the request.因为如果 Hosting 没有转发请求,这就是 Cloud Functions URI 的内容。
You could also try:你也可以试试:
app.post('/app/video/token', (req, res) => { ... })
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.