首页 > 解决方案 > 错误 R10(启动超时)-> Web 进程在 Heroku 启动后 60 秒内无法绑定到 $PORT

问题描述

问题

我正在尝试在 heroku 中托管我的 MERN 堆栈应用程序并反复出现此错误。我无法理解导致问题的原因

构建日志取自heroku logs --tail

2021-04-19T10:48:00.000000+00:00 app[api]: Build started by user soumalyabhattacharya6@gmail.com
2021-04-19T10:50:15.718383+00:00 heroku[web.1]: State changed from crashed to starting
2021-04-19T10:50:15.442857+00:00 app[api]: Release v8 created by user soumalyabhattacharya6@gmail.com
2021-04-19T10:50:15.442857+00:00 app[api]: Deploy b1f9e6f1 by user soumalyabhattacharya6@gmail.com
2021-04-19T10:50:15.000000+00:00 app[api]: Build succeeded
2021-04-19T10:50:34.808498+00:00 heroku[web.1]: Starting process with command `npm start`
2021-04-19T10:50:39.377743+00:00 app[web.1]:
2021-04-19T10:50:39.377769+00:00 app[web.1]: > protium@1.0.0 start /app
2021-04-19T10:50:39.377770+00:00 app[web.1]: > node index.js
2021-04-19T10:50:39.377770+00:00 app[web.1]:
2021-04-19T10:50:39.664885+00:00 app[web.1]: Server running on port: 5000
2021-04-19T10:51:35.368111+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2021-04-19T10:51:35.549055+00:00 heroku[web.1]: Stopping process with SIGKILL
2021-04-19T10:51:35.775090+00:00 heroku[web.1]: Process exited with status 137
2021-04-19T10:51:35.875892+00:00 heroku[web.1]: State changed from starting to crashed

我的项目结构是

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----        19-04-2021  11:23 AM                FrontEnd
d-----        19-04-2021  01:36 PM                node_modules
-a----        19-04-2021  02:22 PM              0 .env
-a----        19-04-2021  02:27 PM            527 .gitignore
-a----        19-04-2021  04:17 PM            975 index.js
-a----        19-04-2021  02:22 PM          23373 package-lock.json
-a----        19-04-2021  04:42 PM            763 package.json
-a----        19-04-2021  02:45 PM             14 Procfile

其中FrontEnd有一个使用create-react-app.

我的index.js文件如下所示:

require("dotenv").config();
const express = require('express');
const path =  require('path');
const app = express();
const port = process.env.port || 5000;
app.use(express.json());
app.use(express.urlencoded({extended: true}));

app.get("/",(req,res)=>{
    res.json({'messege': "Welcome to Backend of Protium"});
});
app.get('/api/hello', (req, res) => {
  res.send({ express: 'Hello From Express' });
});
app.post('/api/world', (req, res) => {
  res.send(
    `I received your POST request. This is what you sent me: ${req.body.post}`,
  );
});
if (process.env.NODE_ENV === 'production') {
    // Serve any static files
    app.use(express.static(path.join(__dirname, 'FrontEnd/build')));
    // Handle React routing, return all requests to React app
    app.get('*', function(req, res) {
      res.sendFile(path.join(__dirname, 'FrontEnd/build', 'index.html'));
    });
  }
app.listen(port,()=>{console.log(`Server running on port: ${port}`)});

我的 package.json 文件root-directory如下所示:

    {
      "name": "protium",
      "version": "1.0.0",
      "description": "Project Management Platfrom Backend",
      "main": "index.js",
      "dependencies": {
        "dotenv": "^8.2.0",
        "express": "^4.17.1",
        "jsonwebtoken": "^8.5.1",
        "mongodb": "^3.6.6",
        "node-fetch": "^2.6.1"
      },
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1",
        "start": "node index.js",
        "postinstall": "cd FrontEnd && npm install && npm run build"
        
      },
      "repository": {
        "type": "git",
        "url": "git+https://github.com/soumalyatheking22012001/Protium-Backend.git"
      },
      "author": "Soumalya Bhattacharya",
      "license": "ISC",
      "bugs": {
        "url": "https://github.com/soumalyatheking22012001/Protium-Backend/issues"
      }
    }

看起来像package.json这样:root/FrontEnd

{
  "name": "protium",
  "version": "0.1.0",
  "description": "Project Management Platfrom Frontend",
  "private": true,
  "author": "Soumalya Bhattacharya",
  "dependencies": {
    "@material-ui/core": "^4.11.3",
    "@material-ui/icons": "^4.11.2",
    "@n8tb1t/use-scroll-position": "^2.0.3",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-router-dom": "^5.2.0",
    "react-scripts": "4.0.3",
    "sass": "^1.32.8",
    "swiper": "^6.5.6",
    "web-vitals": "^1.0.1"
  },
  "devDependencies": {
    "@testing-library/jest-dom": "^5.11.4",
    "@testing-library/react": "^11.1.0",
    "@testing-library/user-event": "^12.1.10"
  },
  "repository": {
    "type": "git",
    "url": "https://github.com/soumalyatheking22012001/Protium-Frontend.git"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "bugs": {
    "url": "https://github.com/soumalyatheking22012001/Protium-Frontend.git/issues"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "proxy": "0.0.0.0"
}

我还没有实现从前端到后端的任何 API 调用。

标签: javascriptexpressherokumernheroku-cli

解决方案


推荐阅读