Buffer.concat()
Concatenates a list of buffers into a single new buffer.
Syntax
Buffer.concat(list[, totalLength]) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
list | Buffer[] | Yes | An array of Buffer instances to join. |
totalLength | number | No | Pre-computed total length; avoids an extra pass. |
Returns
Buffer — A new Buffer containing all input bytes in order.
Examples
const a = Buffer.from('foo');
const b = Buffer.from('bar');
console.log(Buffer.concat([a, b]).toString());
Output
foobar
const chunks = [];
req.on('data', (c) => chunks.push(c));
req.on('end', () => {
const body = Buffer.concat(chunks).toString();
console.log(body.length);
});
Output
512
Notes
The standard way to assemble a body from streamed data chunks.
Passing `totalLength` skips a length-summing pass for a minor speed
gain. An empty list returns a zero-length buffer.