NodeJS Converting text buffer to viewable HTML Page

Viewed 1645

I am receiving data reading from an S3 bucket, which will contain .html files. they are being received by Node like so:

{"type":"Buffer","data":[60,104,116,109,108,32,120,109,108,110,115,58,118,61,34,117,114 ....]}

Is there any way you can take this buffer and use it somewhere to insert directly into like an iFrame src? Or what would be the best way to go about taking this buffer and being able to show it as an HTML page?

Was also thinking of taking the buffer and writing the file with fs.writeFile(...) and using the local path as the src.

Any suggestions / advice would be appreciated thank you in advance.

2 Answers

You can convert the ArrayBuffer to a String easily. Given your partial Buffer, it would look like this:

const dataFromS3 = {"type":"Buffer","data":[60,104,116,109,108,32,120,109,108,110,115,58,118,61,34,117,114]};
const output = String.fromCharCode.apply(null, dataFromS3.data);

console.log(output);

That would work as well

const d = {"type":"Buffer","data":[60,104,116,109,108,32,120,109,108,110,115,58,118,61,34,117,114]}

const r =  Buffer.from(d.data).toString()
console.log(r) // <html xmlns:v="ur
Related