I wrote a little Node.js script to scrape data from a website on which I'm iterating through pages to extract structured data.
The data I extract for each page is an a form of an array of objects.
I thought I could use fs.createWriteStream() method to create a writable stream on which I could write the data incrementally after each page extraction.
Apparently, you can only write a String or a Buffer to the stream, so I'm doing something like this:
output.write(JSON.stringify(operations, null, 2));
But in the end, once I close the stream, the JSON is malformatted because obvisously I just appended every array of each page one after the other, resulting in something looking like this:
[
{ ... }, /* data for page 1 */
{ ... }
][ /* => here is the problem */
{ ... }, /* data for page 2 */
{ ... }
]
How could I proceed to actually append the arrays into the output instead of chaining them? Is it even do-able?