Node.js Heroku:加载资源失败:服务器响应状态为404(未找到)
我在本地机器上有angular 6应用程序,一切都按照我的要求完美运行,完成项目后我将其部署到heroku,当我运行我的应用程序时,heroku中的应用程序链接如下: 正如您所看到的,我在控制台浏览器中遇到以下错误 加载资源失败:服务器响应的状态为404未找到 这是我在github中的应用程序结构 为了快速参考,这里是server.jsNode.js Heroku:加载资源失败:服务器响应状态为404(未找到),node.js,angular,typescript,express,heroku,Node.js,Angular,Typescript,Express,Heroku,我在本地机器上有angular 6应用程序,一切都按照我的要求完美运行,完成项目后我将其部署到heroku,当我运行我的应用程序时,heroku中的应用程序链接如下: 正如您所看到的,我在控制台浏览器中遇到以下错误 加载资源失败:服务器响应的状态为404未找到 这是我在github中的应用程序结构 为了快速参考,这里是server.js const express = require('express'); const bodyParser = require('body-parser'); c
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const path = require('path');
const app = express();
const port = process.env.PORT || 3000
app.use(express.static(path.join(__dirname, '/majeni/dist/majeni/')));
app.use(bodyParser.json());
app.use(cors());
const forceSSL = function () {
return function (req, res, next) {
if (req.headers['x-forwarded-proto'] !== 'https') {
return res.redirect(
['https://', req.get('Host'), req.url].join('')
);
}
next();
}}app.use(forceSSL());
app.get('/*', function (req, res) {
res.sendFile(path.join(__dirname + '/majeni/dist/majeni/index.html'));
});
app.listen(port, () => {
console.log('Server started on port '+port);
});
这是heroku日志
2018-08-16T17:46:38.891333+00:00应用程序[web.1]:错误:enoint:没有这样的文件或目录,stat'/app/majeni/dist/majeni/index.html'
我的代码怎么了?Heroku找不到dist目录,因为它不是您提交的存储库的一部分。dist目录已添加到.gitignore文件中 提交dist目录通常不是一个好主意,因此我们应该在Heroku上生成它 可以通过在package.json中添加postinstall脚本来实现这一点
可以找到更多信息您是否正在Heroku环境中构建Angular应用程序?您是如何生成dist目录的?不,是cd agency,然后是cd majeni,然后是ng build,正如您在githubRight中看到的目录结构一样,但您是在Heroku环境中这样做的吗?如果您只在本地计算机上执行此操作,那么Heroku将没有该目录的副本。您的签入代码中没有dist目录不在heroku环境中,仅在本地计算机中,在ng build i run git push heroku Master之后。您的dist目录位于您的.gitignore中,因此未提交。最好的选择是提供用于构建项目的安装后脚本
"scripts": {
"postinstall": "ng build"
}