2017-06-06 87 views
0

我正在用NodeJS創建一個聊天應用程序,並且我想部署到Heroku。但是,通過使用Github部署,我得到一個錯誤:An error occurred in the application and your page could not be served. If you are the application owner, check your logs for details.。有誰知道發生了什麼事?如何將NodeJS應用程序部署到Heroku?

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); 
+0

查看'heroku logs -t',它通常會顯示更多關於您的應用崩潰原因的信息。 –

+0

我可以告訴你我的github項目,看看我做錯了嗎?我想要做的就是在heroku中使用Github部署 –

+0

當然,您也可以通過本指南,因爲它可以幫助您將GH部署集成到Heroku中https://devcenter.heroku.com/articles/github-integration –

回答

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的,你會看到你的應用程序運行。

+0

它的工作!非常感謝你! –

相關問題