简体   繁体   English

将快速服务器代码部署为反应应用程序的 Firebase 云功能

[英]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.

相关问题 将 Express 服务器部署到 Firebase 问题 - Deploying Express server to Firebase issue 在 apache 服务器上部署 React (3000) 和 Express (8000) 应用程序 - Deploying React (3000) and Express (8000) app on the apache server 部署 React + NodeJS + Express + MySQL 应用到 Heroku 只部署服务器而不是 React - Deploying a React + NodeJS + Express + MySQL App to Heroku only deploying the Server Not the React 在Google云中部署react app - Deploying react app in google cloud 在 Express JS 中,post() 方法在部署后在 firebase 云 function 中运行良好,但在本地机器上无法运行 - In Express JS, post() method is working perfectly in firebase cloud function after deploying but not working in local machine 在 firebase 上部署 react 前端和 express/nodejs 后端 - Deploying react frontend & express/nodejs backend on firebase 使用webpack和Express.js部署React App - Deploying React App with webpack and Express.js 无法解决的 CORS 错误反应应用程序试图调用 firebase 云 function - Unresolveable CORS Error for react app trying to call firebase cloud function 使用 webpack 服务器部署 React 应用程序 - Deploying react app with webpack server 错误:部署谷歌云 function 时找不到模块“firebase/app” - Error: Cannot find module 'firebase/app' When deploying google cloud function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM