【问题标题】:Node.js Error: EADDRINUSE on Elastic Beanstalk hostNode.js 错误:Elastic Beanstalk 主机上的 EADDRINUSE
【发布时间】:2015-07-21 11:09:41
【问题描述】:

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] || [];
};

【问题讨论】:

标签: javascript node.js amazon-elastic-beanstalk


【解决方案1】:

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

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

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

【讨论】:

  • 谢谢!如何为 EB 设置它?这是我的 Express 配置: /** * Express 配置。 */ app.set('port', process.env.PORT || 1337);
  • @user3464679 你没有设置它。系统给你设置好了。
  • 好的。那么我有什么选择可以避免这个问题呢?这是启动我的 Express 服务器的代码: /** * 启动 Express 服务器。 */ var server = app.listen(app.get('port'), function() { console.log('服务器在 %s 模式下监听端口 %d', app.get('port'), app. get('env')); });
  • app.listen(process.env.PORT)
  • 而不是 app.get('port') ?
猜你喜欢
  • 2016-03-11
  • 2014-11-22
  • 2020-06-18
  • 2019-07-07
  • 2020-05-15
  • 2018-07-02
  • 2020-09-29
  • 1970-01-01
  • 2015-04-26
相关资源
最近更新 更多