Node.js 使用请求模块取消管道文件抛出ESOCKETTIMEDOUT

Node.js 使用请求模块取消管道文件抛出ESOCKETTIMEDOUT,node.js,Node.js,我正在使用express和模块执行一个简单的请求,并通过管道将其响应发送到res: var pipe = request.get({ url: 'url-to-file' }); pipe.on('response', function (response) { req.on('close', function () { // these methods won't work: // pipe.unpipe(); // pipe

我正在使用express和模块执行一个简单的请求,并通过管道将其响应发送到
res

var pipe = request.get({
    url: 'url-to-file'
});

pipe.on('response', function (response) {

    req.on('close', function () {
        // these methods won't work:
        // pipe.unpipe();
        // pipe.end();
        // pipe.finish();
        // pipe.close();
    });

    pipe.on('end', function () {
        // this will never fire if I cancel the request
    });

    res.writeHead(response.statusCode, response.headers);

    pipe.pipe(res);

});
这就像一个符咒,除非我取消下载。end事件不会触发,几秒钟后,将抛出一个eSocketimedOut错误

我怎样才能关上管子?声明我可以调用
.unpipe
,但节点提供的所有内容都是
管道。unpipe
不是函数(使用v0.12.7和4.2.2和5.0.0测试),可能是因为它不是原始节点流。 我还尝试使用诸如end、finish和close之类的事件,但它们都不起作用。

request.get()
不返回纯node.js流,而是从本机
流继承并添加一些自定义方法的
request
对象。您正在寻找的方法是
Request#abort()
()

您的代码示例如下所示:

var pipe = request.get({
    url: 'url-to-file'
});

pipe.on('response', function (response) {

    req.on('close', function () {
        pipe.abort();
    });

    pipe.on('end', function () {
        // this will never fire if I cancel the request
    });

    res.writeHead(response.statusCode, response.headers);

    pipe.pipe(res);

});

您是否在
close
事件中尝试了
pipe.destroy()