我在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"));
});
如果我做错了,请指出。
答案 0 :(得分:2)
嗨,我遇到了同样的问题,这是mongodb网络访问问题。我通过以下方法解决了这个问题:登录MongoDB>选择您的项目>网络访问>添加IP地址>单击“允许从任何地方访问”
答案 1 :(得分:1)
使用
app.use('*', express.static(path.join(__dirname, "client", "build")))
代替
app.use("/*", (req, res) => {
res.send(path.join(__dirname, "client", "build", "index.html"));
});
我遇到类似的错误,不知道为什么,但是解决了问题