我有一个MERN堆栈项目,以前所有与服务器相关的内容都在根级,但是ESLINT、VSCODE和package.json文件的问题意味着我要将与服务器相关的所有内容都转移到它自己的子目录中。
然而,这意味着Heroku现在给了我一些与Express相关的错误,无法提供文件。请在下面的Heroku日志上看到错误的屏幕抓取。
在Heroku上部署应用程序运行良好,没有构建错误。

如何在我的server.js文件中配置Express
// prod only, uses /public in dev env
app.use(express.static('index.html', { root: '../client/build' }) );
app.get('*', (req, res) => {
res.sendFile(`index.html`, { root: '../client/build' });
});我的目录结构
client/
src/
public/
build/
package.json
server/
routes/
models/
server.js
package.json
.gitignore
README.md
package.json // very small file only including scripts to satisfy Heroku根级package.json (文件的全部内容)
{
"scripts": {
"start": "cd server && node server.js",
"build": "cd client && npm run build",
"install-server": "cd server && npm install",
"install-client": "cd client && npm install",
"heroku-postbuild": "npm run install-server && npm run install-client && npm run build"
}
}备注:
当我在本地启动项目时,
发布于 2021-09-03 08:08:40
事实证明,从这个转变过来:
app.use(express.static('index.html', { root: '../client/build' }) );至
app.use(express.static('/app/client/build'))解决了这个问题。
我确实尝试过app.use(express.static('index.html', { root: '/app/client/build' }) );,但这再次破坏了网站。
https://stackoverflow.com/questions/69035024
复制相似问题