簡體   English   中英

如何將NodeJS App部署到Heroku?

[英]How to deploy NodeJS App to Heroku?

我正在用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. 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中運行。 代碼中的問題是,您沒有為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,您應該會看到您的應用程序正在運行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM