所以我有一个在本地主机上运行良好的简单应用程序,但是当我将它部署到 Heroku 时,前端文件不会加载,所以我得到了没有样式或功能的原始 html。浏览器目录是所有未加载的文件所在的位置。我要把我的服务器文件和 package.json 放在下面。我真的很感激任何帮助。谢谢!
服务器.js
const path = require('path');
const http = require('http');
const server = http.createServer();
const express = require('express');
const app = express();
server.on('request', app);
server.listen(process.env.PORT || 5000)
app.use(express.static(path.join(__dirname, 'Browser')));
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname, 'chess.html'));
});
包.JSON
{
"name": "Chess",
"version": "1.0.0",
"description": "One player chess against an AI",
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"engines": {
"node": ">=4.0.0"
},
"dependencies": {
"express": ">=4.13.3"
}
}
文件结构
_Chessjs
+-- app.json
+-- package.json
+-- Procfile
+-- chess.html
+-- server.js
+-- _Broswer
| +-- ai.js
| +-- board.js
| +-- game.js
| +-- piece.js
| +-- style.css
+-- _node_modules
最佳答案
确保将以下内容添加到 package.json 中的脚本中:
“heroku-postbuild”:“cd client && npm install --only=dev && npm install && npm run build”
否则,即使在解决任何问题后,您可能仍然会收到这些错误消息 使用您的 MERN 应用程序。
更多信息请访问以下链接:https://github.com/heroku/heroku-buildpack-nodejs/issues/589
关于javascript - 找不到 Heroku Node 应用程序 404,不加载前端 js 文件或 css,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40625469/