javascript - Node.js 错误 : EADDRINUSE on Elastic Beanstalk host

标签 javascript node.js amazon-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() 尝试绑定(bind)服务器的端口号已被使用。如何尝试其他端口或关闭使用此端口的程序?

该应用程序在生产模式下运行,但一旦我将其部署到服务器上,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] || [];
};

最佳答案

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

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

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

关于javascript - Node.js 错误 : EADDRINUSE on Elastic Beanstalk host,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30159328/

相关文章:

javascript - Joi 数组验证忽略所需的嵌套键

javascript - 使用 Javascript 更改背景图像

javascript - 我怎样才能在我的项目中实现分页

javascript - 如何在客户端运行 PhantomJS?

javascript - 将 UK EndDate 与 UTC 当前时间进行比较?

mongodb - Elastic Beanstalk 中的 Mongos 安装/设置

elasticsearch - 如何在Elasticsearch中仅查询索引的所有文档名称

elasticsearch - Elasticsearch /kibana 从相同索引的 2 个文档中获取数据?

javascript - 在加载到 html 之前,我如何获取图像大小?

javascript - 表达req.body为空。这是最好的方法吗?