我嘗試在 Heroku 上部署我的 MERN 應用程式,但是當嘗試運行“heroku local”時,localhost 5000 顯示一個空頁面,并且控制臺顯示以下錯誤。這些是錯誤:

我已經嘗試了很多解決方案,但沒有一個奏效。
這是來自節點應用程式的 package.json 檔案:
{
"name": "server",
"version": "1.0.0",
"description": "",
"main": "app.js",
"dependencies": {
"axios": "^0.21.1",
"bcrypt": "^5.0.1",
"cookie-parser": "^1.4.5",
"cors": "^2.8.5",
"express": "^4.17.1",
"express-handlebars": "^5.3.3",
"express-session": "^1.17.2",
"mongoose": "^5.13.7",
"nodemon": "^2.0.12",
"passport": "^0.4.1",
"passport-local": "^1.0.0",
"react-router": "^5.2.1"
},
"devDependencies": {},
"engines": {
"node": "14.16"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build":"cd client && npm run build",
"install-client":"cd client && npm install",
"start": "nodemon app.js",
"heroku-postbuild":"cd client && npm run install-client && npm run build"
},
"author": "",
"license": "ISC"
}
這是添加到 server.js 的代碼片段
if (process.env.NODE_ENV == 'production') {
app.use(express.static('client/build'));
}
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'client', 'build', 'index.html'))
});
這是來自 react 應用程式的 package.json
{
"name": "client",
"version": "0.1.0",
"private": true,
"proxy": "http://localhost:5000",
"dependencies": {
"@material-ui/core": "^4.12.3",
"@material-ui/icons": "^4.11.2",
"@testing-library/jest-dom": "^5.14.1",
"@testing-library/react": "^11.2.7",
"@testing-library/user-event": "^12.8.3",
"axios": "^0.21.1",
"history": "^5.0.1",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-redux": "^7.2.4",
"react-router": "^5.2.1",
"react-router-dom": "^5.2.1",
"react-scripts": "4.0.3",
"web-vitals": "^1.1.2"
},
"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"
]
}
}
uj5u.com熱心網友回復:
我想問題可能是server.js檔案不能正常訪問build的檔案夾frontend/client。你能洗掉所有這些嗎:
if (process.env.NODE_ENV == 'production') {
app.use(express.static('client/build'));
}
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'client', 'build', 'index.html'))
});
并添加這個:
app.use(express.static(path.join(__dirname, './client/build')));
我假設您的client檔案夾和server.js檔案處于同一級別,這就是為什么它是./client...如果不是,請相應地定義路徑路由。請讓我知道它是否有效。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/361261.html
標籤:javascript 节点.js 反应 英雄联盟 默恩
