【问题标题】:Heroku: Something is already running on portHeroku:端口上已经有东西在运行
【发布时间】:2018-12-03 11:05:45
【问题描述】:

我是 Heroku 的新手,我正在尝试部署我的节点服务器和 create-react-app 前端应用程序。一切都在本地运行良好,但是当我尝试在 Heroku 上运行它时,一切都化为乌有。我没有收到任何部署错误,我只在我的 URL 上看到 Cannot GET /

我的日志说:

Something is already running on port 12345

/server/app.js

var express = require('express');
var app = express();
require('dotenv').config();
var mongoose = require('mongoose');
var config = require('./config');
var apiController = require('./controllers/apiController');

var port = process.env.PORT || 3005;

app.use('/', express.static(__dirname + '/public'));

app.set('view engine', 'ejs');

mongoose.connect(config.getDbConnectionString());
apiController(app);

app.listen(port);

package.json

{
  "name": "happy-holidays",
  "version": "1.0.0",
  "description": "",
  "engines": {
    "node": "8.9.4"
  },
  "scripts": {
    "start": "concurrently --kill-others-on-fail \"yarn run server\" \"yarn run client\"",
    "server": "cd server && yarn start",
    "client": "cd react-ui && yarn start"
  },
  "license": "UNLICENSED" ,
  "cacheDirectories": [
    "node_modules",
    "react-ui/node_modules"
  ],
  "dependencies": {
    "@sendgrid/mail": "^6.2.1",
    "axios": "^0.18.0",
    "body-parser": "^1.18.2",
    "bootstrap": "^4.1.1",
    "concurrently": "^3.5.1",
    "dotenv": "^5.0.1",
    "ejs": "^2.6.1",
    "express": "^4.16.3",
    "mongoose": "^5.1.0",
    "react": "^16.3.2",
    "react-bootstrap": "^0.32.1",
    "react-dom": "^16.3.2",
    "react-router-dom": "^4.2.2",
    "react-scripts": "1.1.4",
    "semantic-ui-css": "^2.3.1",
    "semantic-ui-react": "^0.80.2"
  },
  "repository": {
    "type": "git",
    "url": "https://github.com/SaraRandolph/messagez"
  },
  "devDependencies": {}
}

【问题讨论】:

  • 为什么要使用端口12345 而不是3005

标签: javascript heroku


【解决方案1】:

“为什么端口是 12345 而不是 3005?”

因为 HEROKU 会动态分配 PORT。

【讨论】:

  • 我建议不要在答案中使用修辞问题,也不要引用问题。两者都将答案置于被误认为问题的风险中,这在只允许答案的情况下当然是不合适的。
  • 我确实引用了它,"
  • 是的,你“确实引用了它”。并且“我建议……不要引用这个问题。”
【解决方案2】:

试试

const port = process.env.PORT || 5000

这适用于我的应用。

尝试使用 Heroku 的 getting stated app 像这样配置应用

package.jsonProcfile 复制scripts

【讨论】:

  • 我的 app.js 文件中确实有那行代码。我检查了那个链接,我想我有类似的东西。我的Procfileweb: yarn start,我正在思考 然后转到我的package.json 并运行我的启动脚本,该脚本启动我的服务器,然后是我的ui。也许我做事有问题?
  • 我不知道为什么它不起作用,但我建议阅读本指南Getting Started on Heroku with Node.js
【解决方案3】:

不确定您的 react-ui 启动脚本是什么样的,但您可能需要在服务器启动脚本之前先运行它。理想情况下,您只想为您的 react-ui 运行分发构建,然后在您的服务器脚本中提供该构建 ui 包。

【讨论】:

    【解决方案4】:

    同样的问题已经在这里解决了:Strange Create-React-App Heroku Error - Cannot GET /

    这是因为您没有从 express 服务器提供 index.html 文件。

    所以你必须复制到你的 server.js 文件中

      if (process.env.NODE_ENV === 'production') {
      // Exprees will serve up production assets
      app.use(express.static('client/build'));
    
      // Express serve up index.html file if it doesn't recognize route
      const path = require('path');
      app.get('*', (req, res) => {
        res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
      });
    }
    

    【讨论】:

      猜你喜欢
      • 2019-12-24
      • 1970-01-01
      • 2019-04-06
      • 2019-04-08
      • 1970-01-01
      • 1970-01-01
      • 2018-08-07
      • 2021-11-20
      • 2013-03-24
      相关资源
      最近更新 更多