NodeJS + React + Socket.io应用程序无法在Heroku上运行

时间:2015-07-29 10:45:20

标签: node.js heroku socket.io

我使用React编写了一个简单的NodeJs Web应用程序。我在这个应用程序中使用Socket.io将客户端连接到服务器。以下代码适用于我的server.js和连接到服务器的组件:

Server.js:

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var index = require('./routes/index');
var socket = require('./common/socket');
var exphbs = require('express-handlebars');
var app = express();
require('node-jsx').install();

var port = process.env.PORT || 9091;
var io = require('socket.io').listen(app.listen(port, function(){
  console.log('Server is listening on port: ' + port);
}));

io.sockets.on('connection', function(sock){
  socket(sock , io);
});

io.configure(function () {
  io.set("transports", ["xhr-polling"]);
  io.set("polling duration", 10);
});

// view engine setup
app.engine('handlebars', exphbs({defaultLayout: 'main', extname: '.handlebars'}));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'handlebars');


// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));


app.use('/', index);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
  app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    console.log(err.message);
    res.render('error', {
      message: err.message,
      error: err
    });
  });
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
  res.status(err.status || 500);
  res.render('error', {
    message: err.message,
    error: {}
  });
});


module.exports = app;

Component.js:

'use strict';
var React = require('react');
var io = require('socket.io-client');
var socket;
var UserRow = require('./userRow');
var SimpleUserList = React.createClass({

    displayName: 'SimpleUserList',

    componentDidMount: function() {
        socket = io.connect();
        socket.on('userList', function(data) {
            console.log('userlist received');
        }.bind(this));

    },

    componentWillUnmount: function() {
        socket.close();
    },
    render: function() {
        return (<div></div>);
    }
module.exports = SimpleUserList;

main.handlebars:

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" type="text/css" href="stylesheets/style.css">
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

</head>
<body>
    {{{ body }}}
    <script src="https://cdn.socket.io/socket.io-1.0.0.js"></script>
    <script src="javascripts/bundle.js"></script>
</body>
</html>

句柄栏并不重要,我只想显示我在html文件中包含的脚本文件。

知道为什么我无法连接到heroku服务器吗?

heroku日志:

2015-07-29T10:59:30.153481+00:00 app[web.1]: > npm run-script build | node server.js 
2015-07-29T10:59:30.153482+00:00 app[web.1]: 
2015-07-29T10:59:39.356552+00:00 app[web.1]: /app/server.js:22
2015-07-29T10:59:39.356556+00:00 app[web.1]: io.configure(function () {
2015-07-29T10:59:39.356558+00:00 app[web.1]:    ^
2015-07-29T10:59:39.356560+00:00 app[web.1]: TypeError: undefined is not a function
2015-07-29T10:59:39.356561+00:00 app[web.1]:     at Object.<anonymous> (/app/server.js:22:4)
2015-07-29T10:59:39.356565+00:00 app[web.1]:     at Object.Module._extensions..js (module.js:478:10)
2015-07-29T10:59:39.356590+00:00 app[web.1]:     at Module.load (module.js:355:32)
2015-07-29T10:59:39.356595+00:00 app[web.1]:     at startup (node.js:129:16)
2015-07-29T10:59:39.356563+00:00 app[web.1]:     at Module._compile (module.js:460:26)
2015-07-29T10:59:39.356593+00:00 app[web.1]:     at Function.Module.runMain (module.js:501:10)
2015-07-29T10:59:39.356596+00:00 app[web.1]:     at node.js:814:3
2015-07-29T10:59:39.356592+00:00 app[web.1]:     at Function.Module._load (module.js:310:12)
2015-07-29T10:59:44.937645+00:00 app[web.1]: npm ERR! Linux 3.13.0-49-generic
2015-07-29T10:59:44.817354+00:00 app[web.1]: util.print: Use console.log instead
2015-07-29T10:59:44.932214+00:00 app[web.1]: 
2015-07-29T10:59:44.938157+00:00 app[web.1]: npm ERR! argv "/app/.heroku/node/bin/node" "/app/.heroku/node/bin/npm" "start"
2015-07-29T10:59:44.938399+00:00 app[web.1]: npm ERR! node v0.12.7
2015-07-29T10:59:44.938826+00:00 app[web.1]: npm ERR! npm  v2.11.3
2015-07-29T10:59:44.939022+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2015-07-29T10:59:44.939233+00:00 app[web.1]: npm ERR! simple-user-list@0.0.0 start: `npm run-script build | node server.js `
2015-07-29T10:59:44.939499+00:00 app[web.1]: npm ERR! Exit status 1
2015-07-29T10:59:44.939661+00:00 app[web.1]: npm ERR! 
2015-07-29T10:59:44.939844+00:00 app[web.1]: npm ERR! Failed at the simple-user-list@0.0.0 start script 'npm run-script build | node server.js '.
2015-07-29T10:59:44.940270+00:00 app[web.1]: npm ERR! This is most likely a problem with the simple-user-list package,
2015-07-29T10:59:44.940497+00:00 app[web.1]: npm ERR! not with npm itself.
2015-07-29T10:59:44.940649+00:00 app[web.1]: npm ERR! Tell the author that this fails on your system:
2015-07-29T10:59:44.940834+00:00 app[web.1]: npm ERR!     npm run-script build | node server.js 
2015-07-29T10:59:44.940988+00:00 app[web.1]: npm ERR! You can get their info via:
2015-07-29T10:59:44.941492+00:00 app[web.1]: npm ERR! There is likely additional logging output above.
2015-07-29T10:59:44.944584+00:00 app[web.1]: 
2015-07-29T10:59:44.941149+00:00 app[web.1]: npm ERR!     npm owner ls simple-user-list
2015-07-29T10:59:44.944830+00:00 app[web.1]: npm ERR! Please include the following file with any support request:
2015-07-29T10:59:44.944952+00:00 app[web.1]: npm ERR!     /app/npm-debug.log
2015-07-29T10:59:45.817817+00:00 heroku[web.1]: Process exited with status 1
2015-07-29T10:59:45.831953+00:00 heroku[web.1]: State changed from starting to crashed
2015-07-29T10:59:47.593870+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=simple-user-list.herokuapp.com request_id=bd0cf3d6-0b8b-4fc0-aa37-a940cd5f328f fwd="50.19.169.132" dyno= connect= service= status=503 bytes=
2015-07-29T10:59:57.607136+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=simple-user-list.herokuapp.com request_id=8f80ec43-0eb8-4375-9341-7c1c2ecb81f0 fwd="50.19.169.132" dyno= connect= service= status=503 bytes=
2015-07-29T11:00:07.626517+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=simple-user-list.herokuapp.com request_id=4ee95d7b-e757-4590-b9e9-f48f491a3654 fwd="50.19.169.132" dyno= connect= service= status=503 bytes=
2015-07-29T11:00:17.650806+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=simple-user-list.herokuapp.com request_id=bdebebe0-6b5b-444b-81f2-fb34d3b00bb4 fwd="50.19.169.132" dyno= connect= service= status=503 bytes=
2015-07-29T11:00:27.660163+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=simple-user-list.herokuapp.com request_id=289949c3-1dac-4f3c-bb89-f6093da2c997 fwd="50.19.169.132" dyno= connect= service= status=503 bytes=
2015-07-29T11:00:37.669828+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=simple-user-list.herokuapp.com request_id=997d267f-81fb-40e4-9c7d-85f9803653f8 fwd="50.19.169.132" dyno= connect= service= status=503 bytes=
2015-07-29T11:06:28.718078+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=simple-user-list.herokuapp.com request_id=c0f10a34-914a-439b-8a59-5ef954d994e8 fwd="207.6.39.42" dyno= connect= service= status=503 bytes=
2015-07-29T11:10:46.273821+00:00 heroku[web.1]: State changed from crashed to starting
2015-07-29T11:10:49.577787+00:00 heroku[web.1]: Starting process with command `npm start`
2015-07-29T11:10:51.591809+00:00 app[web.1]: 
2015-07-29T11:10:51.591830+00:00 app[web.1]: > simple-user-list@0.0.0 start /app
2015-07-29T11:10:51.591832+00:00 app[web.1]: > npm run-script build | node server.js 
2015-07-29T11:10:51.591833+00:00 app[web.1]: 
2015-07-29T11:10:57.340680+00:00 app[web.1]: /app/server.js:22
2015-07-29T11:10:57.340683+00:00 app[web.1]: io.configure(function () {
2015-07-29T11:10:57.340685+00:00 app[web.1]:    ^
2015-07-29T11:10:57.340687+00:00 app[web.1]: TypeError: undefined is not a function
2015-07-29T11:10:57.340689+00:00 app[web.1]:     at Object.<anonymous> (/app/server.js:22:4)
2015-07-29T11:10:57.340692+00:00 app[web.1]:     at Object.Module._extensions..js (module.js:478:10)
2015-07-29T11:10:57.340693+00:00 app[web.1]:     at Module.load (module.js:355:32)
2015-07-29T11:10:57.340690+00:00 app[web.1]:     at Module._compile (module.js:460:26)
2015-07-29T11:10:57.340695+00:00 app[web.1]:     at Function.Module._load (module.js:310:12)
2015-07-29T11:10:57.340696+00:00 app[web.1]:     at Function.Module.runMain (module.js:501:10)
2015-07-29T11:10:57.340697+00:00 app[web.1]:     at startup (node.js:129:16)
2015-07-29T11:10:57.340699+00:00 app[web.1]:     at node.js:814:3
2015-07-29T11:11:02.957215+00:00 app[web.1]: util.print: Use console.log instead
2015-07-29T11:11:03.080961+00:00 app[web.1]: npm ERR! Linux 3.13.0-57-generic
2015-07-29T11:11:03.088435+00:00 app[web.1]: npm ERR! Please include the following file with any support request:
2015-07-29T11:11:03.088566+00:00 app[web.1]: npm ERR!     /app/npm-debug.log
2015-07-29T11:11:03.074299+00:00 app[web.1]: 
2015-07-29T11:11:03.081974+00:00 app[web.1]: npm ERR! argv "/app/.heroku/node/bin/node" "/app/.heroku/node/bin/npm" "start"
2015-07-29T11:11:03.082248+00:00 app[web.1]: npm ERR! node v0.12.7
2015-07-29T11:11:03.082698+00:00 app[web.1]: npm ERR! npm  v2.11.3
2015-07-29T11:11:03.082883+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2015-07-29T11:11:03.083205+00:00 app[web.1]: npm ERR! simple-user-list@0.0.0 start: `npm run-script build | node server.js `
2015-07-29T11:11:03.083354+00:00 app[web.1]: npm ERR! Exit status 1
2015-07-29T11:11:03.083515+00:00 app[web.1]: npm ERR! 
2015-07-29T11:11:03.083669+00:00 app[web.1]: npm ERR! Failed at the simple-user-list@0.0.0 start script 'npm run-script build | node server.js '.
2015-07-29T11:11:03.084247+00:00 app[web.1]: npm ERR! not with npm itself.
2015-07-29T11:11:03.084376+00:00 app[web.1]: npm ERR! Tell the author that this fails on your system:
2015-07-29T11:11:03.083969+00:00 app[web.1]: npm ERR! This is most likely a problem with the simple-user-list package,
2015-07-29T11:11:03.085055+00:00 app[web.1]: npm ERR! You can get their info via:
2015-07-29T11:11:03.084565+00:00 app[web.1]: npm ERR!     npm run-script build | node server.js 
2015-07-29T11:11:03.085058+00:00 app[web.1]: npm ERR!     npm owner ls simple-user-list
2015-07-29T11:11:03.085128+00:00 app[web.1]: npm ERR! There is likely additional logging output above.
2015-07-29T11:11:03.088091+00:00 app[web.1]: 
2015-07-29T11:11:03.853402+00:00 heroku[web.1]: Process exited with status 1
2015-07-29T11:11:03.866414+00:00 heroku[web.1]: State changed from starting to crashed
2015-07-29T11:16:15.196180+00:00 heroku[slug-compiler]: Slug compilation started
2015-07-29T11:16:15.196195+00:00 heroku[slug-compiler]: Slug compilation finished
2015-07-29T11:16:15.153339+00:00 heroku[api]: Deploy a6fb15c by aryan.hosseinzadeh@gmail.com
2015-07-29T11:16:15.153339+00:00 heroku[api]: Release v15 created by aryan.hosseinzadeh@gmail.com
2015-07-29T11:16:15.571381+00:00 heroku[web.1]: State changed from crashed to starting
2015-07-29T11:16:19.908038+00:00 heroku[web.1]: Starting process with command `npm start`
2015-07-29T11:16:22.344144+00:00 app[web.1]: 
2015-07-29T11:16:22.344166+00:00 app[web.1]: > simple-user-list@0.0.0 start /app
2015-07-29T11:16:22.344168+00:00 app[web.1]: > npm run-script build | node server.js 
2015-07-29T11:16:22.344170+00:00 app[web.1]: 
2015-07-29T11:16:28.997464+00:00 app[web.1]: Option polling duration is not valid. Please refer to the README.
2015-07-29T11:16:29.378774+00:00 heroku[web.1]: State changed from starting to up
2015-07-29T11:16:29.671752+00:00 app[web.1]: Server is listening on port: 9807
2015-07-29T11:16:30.880399+00:00 heroku[router]: at=info method=GET path="/" host=simple-user-list.herokuapp.com request_id=8286e365-9790-487e-9b71-a69a4de800fc fwd="54.144.80.213" dyno=web.1 connect=6ms service=227ms status=200 bytes=2017
2015-07-29T11:16:30.910262+00:00 app[web.1]: GET / 200 252.009 ms - 1821
2015-07-29T11:16:34.120554+00:00 app[web.1]: util.print: Use console.log instead

1 个答案:

答案 0 :(得分:0)

所以,它已经解决了。

无需明确引用{host}/socket/socket.io,也无需指定io.config(至少到目前为止),也无需在客户端指定主机地址({{ 1}}足够好了。