Node.js错误:Elastic Beanstalk主机上的EADDRINUSE

时间:2015-05-11 03:42:00

标签: javascript node.js elastic-beanstalk

我对node.js有以下错误消息:

events.js:72 
throw er; // Unhandled 'error' event 
^ 
Error: listen EADDRINUSE 
at errnoException (net.js:904:11) 
at Server._listen2 (net.js:1042:14) 
at Server.wrappedListen2 [as _listen2] (/Users/rakutza/Documents/ParcelPuppy/PP/node_modules/newrelic/lib/instrumentation/core/net.js:16:46) 
at listen (net.js:1064:10) 
at Server.listen (net.js:1138:5) 
at EventEmitter.app.listen (/Users/rakutza/Documents/ParcelPuppy/PP/node_modules/express/lib/application.js:559:24) 
at Object.<anonymous> (/Users/rakutza/Documents/ParcelPuppy/PP/app.js:109:18) 
at Module._compile (module.js:456:26) 
at Object.Module._extensions..js (module.js:474:10) 
at Module.load (module.js:356:32)

显然,listen()尝试绑定服务器的端口号已在使用中。如何使用此端口尝试其他端口或关闭程序?

该应用程序正在生产模式下工作,但一旦我在服务器上部署它,AWS BeanStalk就会崩溃。

var slice = Array.prototype.slice;

/**
 * EventEmitter
 */

function EventEmitter() {
  if (!this._events) this._events = {};
}

EventEmitter.prototype.addListener = function(type, listener) {
  if (!this._events[type]) {
    this._events[type] = listener;
  } else if (typeof this._events[type] === 'function') {
    this._events[type] = [this._events[type], listener];
  } else {
    this._events[type].push(listener);
  }
  this._emit('newListener', [type, listener]);
};

EventEmitter.prototype.on = EventEmitter.prototype.addListener;

EventEmitter.prototype.removeListener = function(type, listener) {
  var handler = this._events[type];
  if (!handler) return;

  if (typeof handler === 'function' || handler.length === 1) {
    delete this._events[type];
    this._emit('removeListener', [type, listener]);
    return;
  }

  for (var i = 0; i < handler.length; i++) {
    if (handler[i] === listener || handler[i].listener === listener) {
      handler.splice(i, 1);
      this._emit('removeListener', [type, listener]);
      return;
    }
  }
};

EventEmitter.prototype.off = EventEmitter.prototype.removeListener;

EventEmitter.prototype.removeAllListeners = function(type) {
  if (type) {
    delete this._events[type];
  } else {
    this._events = {};
  }
};

EventEmitter.prototype.once = function(type, listener) {
  function on() {
    this.removeListener(type, on);
    return listener.apply(this, arguments);
  }
  on.listener = listener;
  return this.on(type, on);
};

EventEmitter.prototype.listeners = function(type) {
  return typeof this._events[type] === 'function'
    ? [this._events[type]]
    : this._events[type] || [];
};

1 个答案:

答案 0 :(得分:1)

您的代码中没有任何地方显示您在网络套接字上的监听位置......

在任何情况下,对于Beanstalk,您应该使用PORT环境变量,可通过process.env.PORT访问。如果您不这样做,则无法保证使用Beanstalk希望您收听的端口。

Beanstalk实例在Node.js应用程序前面有自己的Nginx代理。您的应用无法直接访问。这可能是您遇到碰撞的原因。