如何在Node.js中closureshttpsstream
我正在通过https加载一个.ndjson
文件。 我想在阅读100行文件后closures它。
const amount = 100; https.get(url, (res) => { var { statusCode } = res; if (statusCode !== 200) { throw new Error(`Request Failed.\n Status Code: ${statusCode}`); } res.setEncoding('utf8'); let rows = []; res .pipe(ndjson.parse()) .on('data', function (obj) { rows.push(obj); if (rows.length === amount) { this.end(); } }) .on('end', () => { resolve(rows); }); }).on('error', (e) => { throw new Error(e.message); });
但是,我已经尝试closuresstream的每一个方法,同样的错误信息出现:
Error: Could not parse row {"username"... at DestroyableTransform.parseRow [as mapper] (C:\Users\thoma\Documents\GitHub\test\node_modules\ndjson\index.js:19:28) at DestroyableTransform.flush [as _flush] (C:\Users\thoma\Documents\GitHub\test\node_modules\split2\index.js:44:21) at DestroyableTransform.<anonymous> (C:\Users\thoma\Documents\GitHub\test\node_modules\readable-stream\lib\_stream_transform.js:138:49) at Object.onceWrapper (events.js:314:30) at emitNone (events.js:105:13) at DestroyableTransform.emit (events.js:207:7) at prefinish (C:\Users\thoma\Documents\GitHub\test\node_modules\readable-stream\lib\_stream_writable.js:596:14) at finishMaybe (C:\Users\thoma\Documents\GitHub\test\node_modules\readable-stream\lib\_stream_writable.js:604:5) at afterWrite (C:\Users\thoma\Documents\GitHub\test\node_modules\readable-stream\lib\_stream_writable.js:470:3) at _combinedTickCallback (internal/process/next_tick.js:144:20)
而且这个stream在没有强制closures的情况下工作的很好,所以它和ndjson
文件没有关系。 是否可以在请求中间closuresstream?
有几个解决scheme:
- 发送一个头来closures连接:
this.set("Connection", "close")
- 结束stream感谢
this.end()
我不知道哪一个是最好的。
所以,在你的例子中,你有一个问题,因为this
(我build议你看看这个链接)。 试试下面的代码:
res .pipe(ndjson.parse()) .on('data', obj => { rows.push(obj); if (rows.length === amount) { this.end(); } }) .on('end', () => { resolve(rows); });