Node.js - 事件js 72抛出未处理的'错误'事件

use*_*893 59 node.js

我是Node.js的新手,希望使用流程运行程序.对于其他程序,我不得不同时启动一个服务器(mongodb,redis等),但我不知道我是否应该用这个运行一个.请让我知道我哪里出错了,以及如何纠正这个问题.提前致谢.

这是该计划:

var http = require('http'),
feed = 'http://isaacs.iriscouch.com/registry/_changes?feed=continuous';


function decide(cb) {
setTimeout(function () {
if (Date.now()%2) { return console.log('rejected'); }        
cb();
}, 2000);
}

http.get(feed, function (res) {

decide(res.pipe.bind(res, process.stdout));


//using anonymous function instead of bind:
// decide(function () {
//   res.pipe(process.stdout)
// });

});
Run Code Online (Sandbox Code Playgroud)

这是cmd输出:

<b>C:\05-Employing Streams\05-Employing Streams\23-Playing with pipes>node npm_stre
am_piper.js

events.js:72
throw er; // Unhandled 'error' event
          ^
Error: Parse Error
at Socket.socketOnData (http.js:1583:20)
at TCP.onread (net.js:527:27)
</b>
Run Code Online (Sandbox Code Playgroud)

Gan*_*dey 88

关闭nodejs app在另一个shell中运行. 重新启动终端并再次运行程序.


另一台服务器可能也使用了您用于nodejs的相同端口.杀死正在使用的进程nodejs port运行应用程序.

要查找使用port:8000的应用程序的PID

$ fuser 8000/tcp
8000/tcp:            16708
Run Code Online (Sandbox Code Playgroud)

这里PID是16708现在使用kill [PID]命令终止进程

$ kill 16708
Run Code Online (Sandbox Code Playgroud)

  • 如果您的Node应用程序希望连接到不可用的端口,即使它没有被其他进程使用,也会抛出此错误.例如,我的应用程序希望连接到我设置的Vagrant盒子上的端口,但我还没有启动那个盒子. (3认同)

Vu *_*Luu 15

我有同样的问题.我关闭了终端并重新启动了节点.这对我有用.


lam*_*345 3

好吧,你的脚本抛出一个错误,你只需要捕获它(和/或防止它发生)。我有同样的错误,对我来说这是一个已经使用的端口(EADDRINUSE)。