所以我创建了如下所示的路由,
import React from "react";
import ReactDOM from "react-dom";
import MainComponent from "./components/mainComponent";
import LoaderComponent from "./components/loaderComponent";
import * as serviceWorker from "./serviceWorker";
import { Route, BrowserRouter as Router } from "react-router-dom";
ReactDOM.render(
<Router>
<div>
<Route exact path="/home" component={MainComponent} />
<Route exact path="/loader" component={LoaderComponent} />
<Route exact path="/" component={MainComponent} />
</div>
</Router>,
document.getElementById("root")
);
serviceWorker.unregister();
通过
启动应用程序时,路由工作正常npm运行开始
当我点击localhost:3000 / home时,它会呈现 MainComponent 。 但是在构建之后,它会显示无法获取/ home
npm运行构建
通过nodeJs检查构建后的应用程序时,仅默认路由()有效。其余路由在浏览器中引发无法获取错误。
附在nodejs server.js文件下方
const express = require("express");
const bodyParser = require("body-parser");
const path = require("path");
const dotenv = require("dotenv");
dotenv.config();
require("./db/db_connection");
require("./scheduler");
const app = express();
//This is to make sure request body is accessible in express
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
//Router for the api end points
const leaderboardRoutes = require("./routes");
app.use("/api/v1", leaderboardRoutes);
//Views are present inside build folder
app.use(express.static(path.join(__dirname, "build")));
//Application hosted port
const port = process.env.PORT || 3000;
app.listen(port, function () {
console.log(`Application running on port ${port}`);
});
我还将附加package.json文件以供参考,
{
"name": "test-app",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.5.0",
"@testing-library/user-event": "^7.2.1",
"axios": "^0.19.2",
"body-parser": "^1.19.0",
"bootstrap": "^4.4.1",
"connect-timeout": "^1.9.0",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"http": "0.0.1-security",
"material-ui": "^0.20.2",
"mongoose": "^5.9.9",
"node-schedule": "^1.3.2",
"path": "^0.12.7",
"pure-react-carousel": "^1.25.2",
"react": "^16.13.0",
"react-bootstrap": "^1.0.0-beta.17",
"react-circular-progressbar": "^2.0.3",
"react-dom": "^16.13.0",
"react-router-dom": "^5.2.0",
"react-scripts": "3.4.0",
"request": "^2.88.2"
},
"scripts": {
"start": "react-scripts start",
"start-server": "npm run build && nodemon server.js",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
请帮助我了解问题出在哪里或我错过了什么。 预先感谢。
答案 0 :(得分:0)
我没有使用react-scripts
,但是在构建之后,如果显示Cannot GET /*
,那么在我看来,文件将进入build
目录下的某个目录,这意味着路径您尝试访问的内容不完整。您可能需要检查一下并在构建配置中使用。
例如,在我的情况下,我为应用程序使用了安装路径,因此它看起来像:
http://localhost:3000/myapp
,其中myapp
是装载路径。
然后我在Webpack配置public
中使用了目录的名称path
,因为我想在其中放置文件,然后我将publicPath
的使用值是{{1} },这意味着文件将进入myapp
,因此当我点击上面的URL时,服务器会在此路径中查找build/public/myapp
文件并加载该文件。