[英]Mern stack app working on heroku local but not working online when deploy
我在 Heroku 上有我的 mern 堆棧應用程序,以前它運行良好,但現在部署正常但無法正常工作。
所有 API 都工作正常,但前端沒有呈現。
當我訪問主頁時,它說
/app/client/build/index.html
所有 API 都正常工作
這是我的服務器包 json
"name": "***",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"client-install": "cd client && yarn install ",
"server": "nodemon app",
"start": "node app.js",
"client": "cd client && yarn start ",
"dev": "concurrently \"yarn run server \" \"yarn run client\" ",
"heroku-postbuild": "NPM_CONFIG_PRODUCTION=false && cd client && npm install && npm build "
},
"repository": {
"type": "***",
"url": "***"
},
"author": "",
"license": "ISC",
"homepage": "***",
"dependencies": {
"body-parser": "^1.19.0",
"cors": "^2.8.5",
"express": "^4.17.1",
"helmet": "^3.19.0",
"jimp": "^0.6.4",
"morgan": "^1.9.1",
"multer": "^1.4.1"
},
"devDependencies": {
"concurrently": "^4.1.1"
}
}
這是我的客戶端 json 包
{
"name": "***",
"version": "0.1.0",
"private": ***,
"dependencies": {
"@fortawesome/fontawesome-svg-core": "^1.2.19",
"@fortawesome/free-brands-svg-icons": "^5.9.0",
"@fortawesome/free-regular-svg-icons": "^5.9.0",
"@fortawesome/free-solid-svg-icons": "^5.9.0",
"@fortawesome/react-fontawesome": "^0.1.4",
"axios": "^0.19.0",
"bootstrap": "^4.3.1",
"file-saver": "^2.0.2",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-router-dom": "^5.0.1",
"react-scripts": "3.0.1",
"react-toastify": "^5.3.2",
"reactstrap": "^8.0.1"
},
"scripts": {
"start": "react-scripts start",
"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"
]
},
"devDependencies": {
"node-sass": "^4.12.0"
}
}
在這兩者中,我都有開發依賴項,並且在我的 Heroku 后期構建中從 yarn 切換到 npm。
這是我在 app.js 中的路由文件
app.use(express.static(path.join(__dirname, "client", "build")));
app.use("/*", (req, res) => {
res.send(path.join(__dirname, "client", "build", "index.html"));
});
如果我做錯了什么,請指出。
用
app.use('*', express.static(path.join(__dirname, "client", "build")))
代替
app.use("/*", (req, res) => {
res.send(path.join(__dirname, "client", "build", "index.html"));
});
我遇到了類似的錯誤,不知道為什么,但解決了問題
嗨,我遇到了同樣的問題,這是一個 mongodb 網絡訪問問題。 我是這樣解決的:登錄MongoDB>選擇你的項目>網絡訪問>添加IP地址>點擊“允許從任何地方訪問”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.