const buffer1 = Buffer.from('Hello, '); const buffer2 = Buffer.from('world!'); const buffer3 = Buffer.concat([buffer1, buffer2]); console.log(buffer3.toString()); // Output: Hello, world!
const header = Buffer.from('Header'); const message = Buffer.from('Message'); const payload = Buffer.concat([header, message], header.length + message.length); console.log(payload.toString()); // Output: HeaderMessageIn this example, we create a header buffer and a message buffer. We then concatenate those two buffers into a single buffer, but we also pass in the total length of the concatenated buffer as the second argument. This is useful if we want to include the length of the header in the payload. The `Buffer` object is built into Node.js and does not require any specific package library.