简体   繁体   English

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

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

I have angular 6 app in my local machine , everything works perfectly as I want, after finishing the project I deployed it to heroku, when I run my app here is the link to the app in heroku : Testing App 我的本地计算机上有angular 6应用,完成项目后,将其部署到heroku,一切正常运行,当我运行我的应用时,这是heroku中该应用的链接: 测试应用

as you can see I get the following error in console browser 如您所见,我在控制台浏览器中收到以下错误

Failed to load resource: the server responded with a status of 404 (Not Found) 加载资源失败:服务器响应状态为404(未找到)

  1. Here is my app structure in github 这是我在github中的应用程序结构

App repo in github github中的应用回购

  1. for quick reference , Here is server.js 为了快速参考,这是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. Here is heroku logs. 这是heroku日志。

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

What is wrong with my code? 我的代码有什么问题?

Heroku is failing to find the dist directory, because it is not part of your committed repository. Heroku无法找到dist目录,因为它不属于已提交的存储库。 The dist directory has been added to the .gitignore file. dist目录已添加到.gitignore文件中。

It's generally a bad idea to commit the dist directory, so we should generate it on Heroku. 提交dist目录通常是个坏主意,因此我们应该在Heroku上生成它。

You can do this by adding a postinstall script in package.json 您可以通过在package.json添加安装后脚本来完成此操作

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

More info can be found in the Heroku docs 更多信息可以在Heroku文档中找到

暂无
暂无

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

相关问题 Node.js服务器:无法加载资源:服务器以状态404(未找到)响应 - Nodejs server: Failed to load resource: the server responded with a status of 404 (Not Found) ReactJS Heroku 部署错误:无法加载资源:服务器响应状态为 404(未找到) - ReactJS Heroku Deployment Error: Failed to load resource: the server responded with a status of 404 (Not Found) 加载资源失败:服务器从 heroku 中的 mongodb 加载图片时响应状态为 404(未找到) - Failed to load resource: the server responded with a status of 404 (Not Found) while loading pictures from mongodb in heroku 加载资源失败:服务器响应状态为 404(未找到) - Ionic2 - Failed to load resource: the server responded with a status of 404 (Not Found) - Ionic2 加载资源失败:服务器以 404(未找到)状态响应 nodejs - Failed to load resource: the server responded with a status of 404 (Not Found) with nodejs 加载资源失败:服务器响应状态为 404 (Not Found) Express - Failed to load resource: the server responded with a status of 404 (Not Found) Express Heroku 无法 GET / 由于“加载资源失败:服务器使用 node.js 响应状态为 404” - Heroku cannot GET / due to 'failed to load resource: server responded with status of 404' using node.js “加载资源失败:服务器响应状态为 404(未找到)”+“来源……访问控制允许来源不允许。” - "Failed to load resource: the server responded with a status of 404 (Not Found)" + "Origin … is not allowed by Access-Control-Allow-Origin." 加载资源失败:服务器响应状态为 404(未找到),而我的后端接受请求 - Failed to load resource: the server responded with a status of 404 (Not Found) while my backend accepts the request 节点js响应加载资源失败:服务器响应状态为404(未找到) - Node js response Failed to load resource: the server responded with a status of 404 (Not Found)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM