我正在尝试创建一个简单的聊天应用程序,但是heroku给了我错误:
2014-05-09T20:19:43.315159+00:00 heroku[api]: Enable Logplex by zwhitchcox@gmail.com
2014-05-09T20:19:43.315241+00:00 heroku[api]: Release v2 created by zwhitchcox@gmail.com
2014-05-09T20:20:06+00:00 heroku[slug-compiler]: Slug compilation started
2014-05-09T20:20:25.635919+00:00 heroku[api]: Release v3 created by zwhitchcox@gmail.com
2014-05-09T20:20:25.532933+00:00 heroku[api]: Scale to web=1 by zwhitchcox@gmail.com
2014-05-09T20:20:25.635919+00:00 heroku[api]: Deploy 62543a1 by zwhitchcox@gmail.com
2014-05-09T20:20:25+00:00 heroku[slug-compiler]: Slug compilation finished
2014-05-09T20:20:26.536745+00:00 heroku[api]: Scale to web=1 by zwhitchcox@gmail.com
2014-05-09T20:20:27.704168+00:00 heroku[web.1]: Starting process with command `node server.js`
2014-05-09T20:20:29.653123+00:00 app[web.1]: info: socket.io started
2014-05-09T20:21:29.273275+00:00 heroku[web.1]: State changed from starting to crashed
2014-05-09T20:21:29.274054+00:00 heroku[web.1]: State changed from crashed to starting
2014-05-09T20:21:27.960123+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2014-05-09T20:21:27.960449+00:00 heroku[web.1]: Stopping process with SIGKILL
2014-05-09T20:21:29.258964+00:00 heroku[web.1]: Process exited with status 137
2014-05-09T20:21:32.343694+00:00 app[web.1]: info: socket.io started
2014-05-09T20:21:31.092312+00:00 heroku[web.1]: Starting process with command `node server.js`
2014-05-09T20:21:44.023410+00:00 heroku[router]: at=error code=H20 desc="App boot timeout" method=GET path=/ host=boiling-wave-8331.herokuapp.com request_id=19d30ac7-a101-4ace-b993-89d02092be0f fwd="50.193.187.85" dyno= connect= service= status=503 bytes=
2014-05-09T20:22:34.214614+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path=/favicon.ico host=boiling-wave-8331.herokuapp.com request_id=09ffb0f3-2c08-4d12-8b9c-37fa4c112680 fwd="50.193.187.85" dyno= connect= service= status=503 bytes=
2014-05-09T20:22:32.984322+00:00 heroku[web.1]: State changed from starting to crashed
2014-05-09T20:22:32.972699+00:00 heroku[web.1]: Process exited with status 137
2014-05-09T20:22:31.694958+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2014-05-09T20:22:31.695682+00:00 heroku[web.1]: Stopping process with SIGKILL
2014-05-09T20:26:46.594041+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path=/favicon.ico host=boiling-wave-8331.herokuapp.com request_id=7823d4c1-f102-4e64-b47f-c50e5c9452c4 fwd="50.193.187.85" dyno= connect= service= status=503 bytes=
2014-05-09T20:26:46.280531+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path=/ host=boiling-wave-8331.herokuapp.com request_id=1b4b66ba-f5f5-4fca-affc-06b3b6f3dc38 fwd="50.193.187.85" dyno= connect= service= status=503 bytes=
2014-05-09T20:27:21.798690+00:00 heroku[api]: Scale to web=1 by zwhitchcox@gmail.com
2014-05-09T20:27:32.718961+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path=/ host=boiling-wave-8331.herokuapp.com request_id=ca1e9a7a-b7c8-4c67-a064-e069e0940dd6 fwd="50.193.187.85" dyno= connect= service= status=503 bytes=
2014-05-09T20:27:33.050677+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path=/favicon.ico host=boiling-wave-8331.herokuapp.com request_id=50a8f3e7-a60a-4f76-8eb4-7b17c9e26155 fwd="50.193.187.85" dyno= connect= service= status=503 bytes=
但是,每当我使用工头开始时,我的应用程序都能正常工作。
这是我的服务器代码:
var express = require('express'),
app = express(),
server = require('http').createServer(app),
io = require('socket.io').listen(server),
port = 8090,
chatClients = new Object();
server.listen(port);
app.get('/', function (req, res) {
res.sendfile(__dirname+'/index.html');
});
io.sockets.on("connection", function(socket) {
socket.on('message', function(data) {
io.sockets.emit('receiveMessage',{yourMessage:data.message});
});
});
这是我的index.html代码:
<div id="messages"></div>
<input id="message">
<button id="submit">send</button>
<script type="text/javascript" src="/socket.io/socket.io.js"></script>
<script>
socket = null;
socket = io.connect();
socket.on('receiveMessage', function(data) {
var message = document.createElement('p');
message.innerHTML = data.yourMessage;
document.getElementById('messages').appendChild(message);
});
document.getElementById('submit').onclick = function() {
socket.emit('message',{message:document.getElementById('message').value});
}
</script>
任何人都可以帮我一把吗?
答案 0 :(得分:0)
Heroku随机分配端口,您需要确保将环境分开。
var express = require('express'),
app = express(),
server = require('http').createServer(app),
io = require('socket.io').listen(server),
port = (process.env.NODE_ENV == 'production') ? process.env.PORT : 8090,
chatClients = new Object();
server.listen(port);
app.get('/', function (req, res) {
res.sendfile(__dirname+'/index.html');
});
io.sockets.on("connection", function(socket) {
socket.on('message', function(data) {
io.sockets.emit('receiveMessage',{yourMessage:data.message});
});
});
然后执行:heroku config:set NODE_ENV=production
在此处详细了解:https://devcenter.heroku.com/articles/nodejs-support#environment