【问题标题】:Index.html not rendering component when served through Express通过 Express 服务时,Index.html 不呈现组件
【发布时间】:2019-10-12 07:01:19
【问题描述】:

本教程结束 https://www.freecodecamp.org/news/how-to-deploy-a-react-app-with-an-express-server-on-heroku-32244fe5a250/

我已将我的 App.js 更改为以下内容:

import React, { Component } from "react";
import "./App.css";

class App extends Component {
  render() {
    return (
      <div>
        <p>lmfao</p>
      </div>
    );
  }
}

export default App;

现在不幸的是,在运行“npm run dev”后访问 localhost:5000 时,虽然页面标题显示为“React App”且页面源为 index.html ,App 组件没有渲染,也没有任何地方出现错误。

这是访问 localhost:5000 的图片,它给出了 index.html

现在有趣的是,如果我访问 localhost:3000 上的 React 服务器,组件会渲染。

我错过了什么? 非常感谢

P.S 这里是 Server.js

const express = require("express");
const app = express();
const path = require("path");
const port = process.env.PORT || 5000;

// Static file declaration
app.use(express.static(path.join(__dirname, "client/build")));

// production mode
if (process.env.NODE_ENV === "production") {
  app.use(express.static(path.join(__dirname, "client/build")));
  //  app.get('*', (req, res) => {    res.sendfile(path.join(__dirname = 'client/build/index.html'));  })
}

// build mode
app.get("*", (req, res) => {
  res.sendFile(path.join(__dirname, "/client/public/index.html"));
});

// start server
app.listen(port, (req, res) => {
  console.log(`server listening on port: ${port}`);
});

这里是 package.json 中的依赖:

"devDependencies": {
    "eslint": "^6.5.1",
    "eslint-config-standard": "^14.1.0",
    "eslint-plugin-flowtype": "^4.3.0",
    "eslint-plugin-import": "^2.18.2",
    "eslint-plugin-jsx-a11y": "^6.2.3",
    "eslint-plugin-node": "^10.0.0",
    "eslint-plugin-promise": "^4.2.1",
    "eslint-plugin-react": "^7.16.0",
    "eslint-plugin-react-hooks": "^2.1.2",
    "eslint-plugin-standard": "^4.0.1"
  },
  "dependencies": {
    "concurrently": "^5.0.0",
    "express": "^4.17.1",
    "react-router-dom": "^5.1.2"
  }

编辑:根据要求,这是该项目的 github 链接: https://github.com/Dobermensch/ExpressTest.git 您必须为 ExpressTest 文件夹和 client 子文件夹执行 npm install

【问题讨论】:

  • 我要做的第一件事是在 devtools (developers.google.com/web/tools/chrome-devtools/network) 中为每个页面打开网络选项卡,然后刷新它们。确定是否提供相同的内容。然后你会更好地了解从那里去哪里。
  • 你能把代码推到github上并分享链接吗?这将有助于调试。
  • 完成了,请看一下。
  • 看来我必须将客户端文件夹添加为 git 中的子模块。现在已经完成了。让我知道一切是否有效...
  • 由于 express server 是从 build 文件夹运行的,所以你在做这个更改后运行 npm run build 了吗?

标签: javascript reactjs express


【解决方案1】:

出现空白页的最可能原因是无法下载脚本包。

在浏览器中右键单击空白页面并选择“查看页面源代码”或类似菜单。您将看到 .html 文件的内容,包括指向 /some-path/some-name.js 文件的 &lt;script&gt; 标记。无法下载所有或部分文件。要确认是这种情况,只需通过将浏览器指向localhost:5000/some-path/some-name.js 手动下载捆绑包。如果无法下载脚本包,请添加路由到 Express。

【讨论】:

  • 我的 index.html 中没有任何脚本标签
  • 那么就是说明空白页的问题了。您提到的 freecodecamp article 使用 Create React App (CRA)。 CRA 在文件底部创建 index.html&lt;script src="/static/js/&lt;some-name&gt;chunk.js"&gt;&lt;/script&gt;。这些 &lt;name&gt;.chunk.js 文件是 React 脚本包。您在页面上看到的是从包中执行代码的结果。没有捆绑包 = 空白页。
  • 如果 CRA 使用这些捆绑包自动创建 index.html,那么为什么它不在我的 index.html 中?以及如何到达那里?
  • 很有趣...你让我找到了正确的答案 我必须转到客户端文件夹,然后运行 ​​npm run build 然后我再次在 ExpressTest 文件夹中运行服务器(npm run dev)它适用于 localhost:5000
  • 我很高兴它有帮助:)
猜你喜欢
  • 1970-01-01
  • 2018-05-15
  • 2013-09-07
  • 1970-01-01
  • 2017-03-31
  • 1970-01-01
  • 2020-01-08
  • 2019-08-31
  • 1970-01-01
相关资源
最近更新 更多