繁体   English   中英

Heroku:无法加载资源:服务器以状态404(未找到)响应

[英]Heroku: Failed to load resource: the server responded with a status of 404 (Not Found)

我的本地计算机上有angular 6应用,完成项目后,将其部署到heroku,一切正常运行,当我运行我的应用时,这是heroku中该应用的链接: 测试应用

如您所见,我在控制台浏览器中收到以下错误

加载资源失败:服务器响应状态为404(未找到)

  1. 这是我在github中的应用程序结构

github中的应用回购

  1. 为了快速参考,这是server.js

     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); }); 
  2. 这是heroku日志。

2018-08-16T17:46:38.891333 + 00:00 app [web.1]:错误:ENOENT:没有这样的文件或目录,stat'/app/majeni/dist/majeni/index.html'

我的代码有什么问题?

Heroku无法找到dist目录,因为它不属于已提交的存储库。 dist目录已添加到.gitignore文件中。

提交dist目录通常是个坏主意,因此我们应该在Heroku上生成它。

您可以通过在package.json添加安装后脚本来完成此操作

"scripts": {
  "postinstall": "ng build"
}

更多信息可以在Heroku文档中找到

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM