【问题标题】:React Express Built app not working in production modeReact Express 构建的应用程序无法在生产模式下运行
【发布时间】:2022-02-25 16:03:33
【问题描述】:

我有一个 Create React App 在开发模式下运行良好 yarn startyarn build > serve -s build

但是当我将构建文件夹上传到我的远程服务器(在 Planet Hoster 上)时,我收到以下错误:

GEThttps://www.mysite.fr/static/js/main.f79317f8.js
[HTTP/2 404 Not Found 273ms]

GEThttps://www.mysite.fr/static/css/main.43a07738.css
[HTTP/2 404 Not Found 272ms]

这里发生了什么?我一直在工作,但我仍然找不到任何东西......任何帮助都会非常有用!

我的快速服务器启动代码:

const express = require("express");
const path = require("path");
const app = express();

app.use(express.static(path.join(__dirname, "build")));

app.get("/", function (req, res) {
  res.sendFile(path.join(__dirname, "build", "index.html"));
});

[编辑] - 如果可以的话,这是我的 package.json 文件:

{
  "name": "frontend",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.16.1",
    "@testing-library/react": "^12.1.2",
    "@testing-library/user-event": "^13.5.0",
    "axios": "^0.25.0",
    "cors": "^2.8.5",
    "express": "^4.17.2",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-router": "^6.2.1",
    "react-router-dom": "^6.2.1",
    "react-router-hash-link": "^2.4.3",
    "react-scripts": "5.0.0",
    "web-vitals": "^2.1.4",
    "webpack": "^5.67.0"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

【问题讨论】:

  • 构建文件在哪里创建?在您的构建文件夹中?您在路径/build 上使您的构建文件夹静态可用,但您站点中的链接链接到名为static 的文件夹。这里有什么不匹配的地方吗?
  • 我也是这么想的。我的构建文件位于构建文件夹中,位于根目录“/”。
  • 当在服务 -s 构建后的开发模式下,我可以看到“GET localhost:3000/static/js/main.f79317f8.js”运行良好......但在生产中,我不知道为什么但它不工作。 ..
  • 你有不同的 webpack 配置用于生产和开发吗?

标签: reactjs express build create-react-app


【解决方案1】:

我最近又回来了,但我终于找到了解决方案。

为了实现它,我在本地运行 node app.js 这是启动我的前端服务的文件。然后我在日志中发现我的脚本试图“在发送后修改我的站点的标题”。我修复了它,现在它可以工作了。 我的代码可能不是那么干净。

仍然感谢您的帮助!

【讨论】:

    猜你喜欢
    • 2019-07-10
    • 1970-01-01
    • 1970-01-01
    • 2015-06-30
    • 1970-01-01
    • 2018-05-31
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多