Alexander Mills
Alexander Mills

Reputation: 99970

How to append to Buffer in Node.js

Say I have a Buffer:

let b = Buffer.from('');

how can I append to b? Is the only way to create a new Buffer?

b = Buffer.concat([b, z]);

on the same subject, is there a way to create a dynamic sized buffer, or should I use Array instead?

Upvotes: 9

Views: 8425

Answers (1)

Get Off My Lawn
Get Off My Lawn

Reputation: 36299

To create a dynamic buffer use an array then concat the array similar to this:

let chunks = []

stream
  .on('data', chunk => chunks.push(chunk))
  .on('close', () => console.log(Buffer.concat(chunks)))

Upvotes: 14

Related Questions