简体   繁体   English

如何将NodeJS App部署到Heroku?

[英]How to deploy NodeJS App to Heroku?

I am creating a chat app with NodeJS, and I want to deploy to Heroku. 我正在用NodeJS创建一个聊天应用程序,我想部署到Heroku。 However, I get an error: An error occurred in the application and your page could not be served. If you are the application owner, check your logs for details. 但是,我收到一个错误: An error occurred in the application and your page could not be served. If you are the application owner, check your logs for details. An error occurred in the application and your page could not be served. If you are the application owner, check your logs for details. by using the Github deployment. 通过使用Github部署。 Does anyone know what is going on? 有人知道发生了什么吗?

Here is some code to see what I have done. 这是一些代码,看我做了什么。

    {
  "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 (Server) 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); 

I got the app working in a Heroku. 我让该应用程序在Heroku中运行。 The issue in your code is that you're not setting the correct port for heroku to work. 代码中的问题是,您没有为heroku设置正确的端口。 In the code you provided you're setting the correct port by doing 在您提供的代码中,通过执行以下操作来设置正确的端口

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

However, in your GitHub project you're hardcoding the port to 3000 但是,在您的GitHub项目中,您将端口硬编码为3000

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

Instead what you want to do is allow Heroku to define the port or default to 3000 for development like so.. 相反,您想要做的是允许Heroku定义端口或将其默认设置为3000,以便进行开发。

var process = require('process');

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

Once you have that, deploy to Heroku and you should see your app running. 完成后,部署到Heroku,您应该会看到您的应用程序正在运行。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM