我正在编写Node.js脚本来解析Wikidata数据库转储并将它们插入到我自己的数据库中。我在下载文件时遇到了麻烦,因为似乎Node.js的http模块和来自npm的请求模块都过早地结束了响应流。我收到以下错误:
events.js:85
throw er; // Unhandled 'error' event
^
SyntaxError: Unexpected end of input
at parse (native)
at emit (/var/app/current/node_modules/split/index.js:27:17)
at next (/var/app/current/node_modules/split/index.js:48:7)
at Stream.<anonymous> (/var/app/current/node_modules/split/index.js:53:5)
at Stream.stream.write (/var/app/current/node_modules/split/node_modules/through/index.js:26:11)
at Gunzip.ondata (_stream_readable.js:540:20)
at Gunzip.emit (events.js:107:17)
at readableAddChunk (_stream_readable.js:163:16)
at Gunzip.Readable.push (_stream_readable.js:126:10)
at Gunzip.Transform.push (_stream_transform.js:140:32)
我使用以下代码下载数据库转储:
var streamer = request.get("http:/dumps.wikimedia.org/other/wikidata/20150330.json.gz").pipe(require('zlib').createGunzip()).pipe(split(JSON.parse));
streamer.on('data', function(obj){
...
});
无论是使用请求模块还是内置的http模块,错误都是一样的。