【问题标题】:Deploying Hello World react js app on to Heroku将 Hello World react js 应用部署到 Heroku
【发布时间】:2021-02-12 04:36:29
【问题描述】:

我收到一个应用程序错误。这是错误:

2020-10-29T17:02:02.043156+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=helloworld-mastercard.herokuapp.com request_id=640ea1df-832d-41a2-ab8a-90ba603398e9 fwd="37.228.208.88" dyno= connect= service= status=503 bytes= protocol=https
2020-10-29T17:02:05.327275+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=helloworld-mastercard.herokuapp.com request_id=f1f3fe21-e1e5-4a39-9ee5-bf078ff39266 fwd="37.228.208.88" dyno= connect= service= status=503 bytes= protocol=https

我遵循了有关如何执行此操作的 youtube 教程。我添加了一个 server.js,内容如下:

const express = require('express');
const favicon = require('express-favicon');
const path = require('path');
const port = process.env.PORT || 8080;
const app = express();
app.use(favicon(__dirname + '/build/favicon.ico'));
// the __dirname is the current directory from where the script is running
app.use(express.static(__dirname));
app.use(express.static(path.join(__dirname, 'build')));
app.get('/ping', function (req, res) {
 return res.send('pong');
});
app.get('/*', function (req, res) {
  res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
app.listen(port);

我的package.json如下:

{
  "name": "helloworld",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.11.5",
    "@testing-library/react": "^11.1.0",
    "@testing-library/user-event": "^12.1.10",
    "express": "^4.17.1",
    "express-favicon": "^2.0.1",
    "path": "^0.12.7",
    "react": "^17.0.1",
    "react-dom": "^17.0.1",
    "react-scripts": "4.0.0",
    "web-vitals": "^0.2.4"
  },
  "scripts": {
    "start": "node server.js",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },

我已经完成了 npm run build 并且很困惑我哪里出错了

【问题讨论】:

    标签: javascript reactjs express heroku package.json


    【解决方案1】:

    Heroku 需要一个文件调用 Procfile 来执行构建和运行过程。

    在您的情况下,在您应用的主文件夹中创建一个完全命名为 Procfile 的文件并添加以下内容:

    web: node server.js
    

    您可以阅读更多关于 Procfile 的内容here

    此外,要在 Heroku 上运行构建脚本,您需要将其添加到 package.json 文件的脚本部分,如下所示:

    "scripts": {
      "start": "node server.js", // serves the application
      "heroku-postbuild": "webpack -p" // runs after installation
    }
    

    有关如何将 webpack 应用程序部署到 Heroku 的详细信息,请参阅link

    说明 Heroku 会自动安装我们所有的依赖项,并在为我们的应用程序提供服务之前继续执行构建后操作。在这个构建后操作中,我们对 js 文件进行生产打包,并允许 Heroku 通过运行 start 命令启动应用程序。

    【讨论】:

    • 我应该将 package.json 中脚本中的“start”设置为其他内容吗?
    • 这是procfile.txt还是procfile
    • 因为我已经做出了改变,但它仍然给我同样的错误
    • Procfile 始终是一个简单的文本文件,名为 Procfile,没有文件扩展名。例如,Procfile.txt 无效。 Procfile 必须位于应用程序的根目录中。如果放在其他任何地方,它就不起作用。
    • 我对此进行了研究,发现 package.json 中的脚本必须包含 heroku 的代码。我相应地编辑了我的答案。
    猜你喜欢
    • 2021-08-13
    • 1970-01-01
    • 2013-05-01
    • 1970-01-01
    • 2014-12-13
    • 2021-04-07
    • 1970-01-01
    • 2020-10-29
    • 2018-01-10
    相关资源
    最近更新 更多