简体   繁体   中英

How to deploy NodeJS App to Heroku?

I am creating a chat app with NodeJS, and I want to deploy to 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. by using the Github deployment. 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)

    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. The issue in your code is that you're not setting the correct port for heroku to work. 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

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..

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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