部署express服务器代码作为react应用程序的firebase云功能



我有下面的express服务器代码,我想将其作为firebase云函数运行。但是我有这个错误(找不到页面(POST https://vid-chat-app.web.app/video/token 404。我在这里做错了什么?

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);
});

这是我尝试将代码转换为云功能

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)

这是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"
}
}

这是react应用中的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]
);

更新

我已经将firebase.json文件的一部分编辑为

{"源":"视频/令牌";,"函数":"应用程序";},

这似乎奏效了。我创建了一个测试GET请求并测试了地址https://vid-chat-app.web.app/video/token在邮差上。它有效!然而,我现在有以下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)

尝试

app.post('/app', (req, res) => { ... })

因为如果Hosting没有转发请求,那么云函数URI就是这样的。

你也可以试试:

app.post('/app/video/token', (req, res) => { ... })

相关内容

  • 没有找到相关文章

最新更新