【问题标题】:How to deploy NodeJS App to Heroku?如何将 NodeJS 应用程序部署到 Heroku?
【发布时间】:2017-11-06 20:49:58
【问题描述】:

我正在使用 NodeJS 创建一个聊天应用程序,并且我想部署到 Heroku。但是,我收到一个错误:An error occurred in the application and your page could not be served. If you are the application owner, check your logs for details. 使用 Github 部署。有谁知道发生了什么?

这里有一些代码来看看我做了什么。

    {
  "name": "chat",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "node ./lib/index.js",
    "test": "jasmine"
  },
  "dependencies": {
    "express": "~4.13.1",
    "firebase": "^3.9.0",
    "request-promise": "^2.0.1",
    "socket.io": "^1.4.5"
  },
  "devDependencies": {
    "jasmine-sinon": "^0.4.0",
    "jscs": "^2.11.0",
    "proxyquire": "^1.7.4",
    "rewire": "^2.5.1",
    "sinon": "^1.17.3"
  }
}

index.js(服务器)

    var express = require('express');
var app = express();
var path = require('path');
var http = require('http').Server(app);
var io = require('socket.io')(http);

var routes = require('./routes');
var chats = require('./chat');



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

routes.load(app);
chats.load(io);


var port = process.env.PORT || 3000;
app.listen(port);
console.log('Server is listening at port:' + port); 

【问题讨论】:

  • 检查 heroku logs -t 它通常会显示更多信息,说明您的应用崩溃的原因。
  • 我可以给你看看我的github项目,看看我做错了什么吗?我想做的是在heroku中使用Github部署
  • 当然,您也可以阅读本指南,因为它将帮助您将 GH 部署集成到 Heroku devcenter.heroku.com/articles/github-integration
  • @LeoCorrea 我做了,但得到了同样的错误。
  • 错误是什么?链接我到你的github项目,我去看看

标签: node.js git heroku


【解决方案1】:

我让应用程序在 Heroku 中运行。您的代码中的问题是您没有为 heroku 工作设置正确的端口。在您提供的代码中,您正在设置正确的端口

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

但是,在您的 GitHub 项目中,您将端口硬编码为 3000

http.listen(3000, function () {
  console.log('listening on *:3000');
});

相反,您想要做的是允许 Heroku 定义端口或默认为 3000 以便像这样进行开发..

var process = require('process');

var port = process.env.PORT || 3000;
http.listen(port, function() {
  console.log("Server is listening on port: " + port);
});

完成后,部署到 Heroku,您应该会看到您的应用正在运行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-24
    • 2017-07-29
    • 2020-04-22
    • 2020-02-23
    • 2018-08-25
    • 2018-08-21
    • 2014-01-13
    相关资源
    最近更新 更多