如何在Firebase托管上部署React + Express应用程序

时间:2020-10-26 20:32:36

标签: javascript node.js reactjs firebase google-cloud-functions

我试图使用firebase托管(使用express)托管我的react应用。

我检查了functions/index.js。我想使用“ https://blahblahapp.web.app/chart”,所以我使用路由器。

// index.js
const functions = require("firebase-functions");
const express = require("express");
const app = express();
const cors = require("cors");
const bodyParser = require("body-parser");
const chart = require("./routes/chart.js");
const news = require("./routes/news.js");
const firebase = require("firebase");
var admin = require("firebase-admin");

app.use(cors());
app.use(bodyParser.json());
app.use("/chart", chart); //problem one
//functions/routes/chart.js
//...

router.post("/chart", (req, res) => {
  var corp_code = req.body.corp_code;
  let result = 0;
  if (corp_code !== undefined) {
    result = getStockInfo(corp_code);
    res.json(result);
  } else {
    res.json({ stocklist: 0 });
  }
});

我还检查了firebase.json

{
  "hosting": {
    "public": "build",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites": [
      {
        "source": "**",
        "destination": "api"
      }
    ]
  }
}

然后我在这里获取'/ chart'。

//src/Chart.js

const fetchData = async (obj) => {
  const { corp_code, corp_name } = obj;
  let result = 0;
  const response = await fetch(`/chart`, {
    method: "post",
    headers: { "Content-type": "application/json" },
    body: JSON.stringify({
      from: "chart",
      corp_name: corp_name,
      corp_code: corp_code,
    }),
  });
  const stocklist = await response.json();
  return stocklist["stocklist"];
};

但是在npm run buildfirebase deploy之后,出现了404错误。

/ chart:1无法加载资源:服务器响应状态为404()

我真的很想知道我的代码有什么问题。我以为我已经尽力了,但仍然没用。

0 个答案:

没有答案