【发布时间】:2019-10-12 07:01:19
【问题描述】:
我已将我的 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