我正在尝试使用node.js创建代理更新代码,而IM会得到此错误:
events.js:180
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'listener', 'Function');
^
TypeError [ERR_INVALID_ARG_TYPE]: The "listener" argument must be of type Function
at _addListener (events.js:180:11)
at WriteStream.addListener (events.js:240:10)
at WriteStream.close (fs.js:2298:10)
at WriteStream.<anonymous> (/Users/camden/Desktop/proxyupdate/u.js:9:15)
at WriteStream.emit (events.js:164:20)
at finishMaybe (_stream_writable.js:616:14)
at afterWrite (_stream_writable.js:467:3)
at onwrite (_stream_writable.js:457:7)
at fs.write (fs.js:2242:5)
at FSReqWrap.wrapper [as oncomplete] (fs.js:703:5)
这是我的代码:
var UpdateProxyList = function(sourceURL, destinationPath) {
var HTTP = require("http");
var FS = require("fs");
var File = FS.createWriteStream(destinationPath);
HTTP.get(sourceURL, function(response) {
response.pipe(File);
File.on('finish', function() {
File.close();
});
File.on('error', function(error) {
FS.unlink(destinationPath);
})
});
}
UpdateProxyList("http://www.example.com/proxy.txt", "myproxylist.txt");
im在macos sierra上使用node.js v9.3.0。
显然,当我使用node.js v8.9.3时,它可以正常工作
在v8.9.3和v9.3.0之间,WriteStream.prototype.close
的实现已更改。
在v8.9.3中,它是对ReadStream.prototype.close
的参考,为此是可选的。
在v9.3.0中,现在是一种单独的方法,除其他外,它发出了close
事件:
WriteStream.prototype.close = function(cb) {
if (this._writableState.ending) {
this.on('close', cb);
return;
}
...
};
您获得的错误是由 this.on('close', cb)
引起的,需要 Function
second参数,该参数未在您的代码中传递。
我不确定您是否真的需要在您的情况下使用finish
处理程序,因为.pipe()
代码将在内部完成可写的处理。