I have an extremely large JSON Object that I wish to send to my ExpressJS Server as a blob. I was able to create the blob on the frontend but I'm not sure how to convert the blob back to the original JSON Object on the backend.
Here is my large JSON Object and how I converted it on the frontend:
let largeObject = {
...... //Redacted
};
const largeObjectStringified = JSON.stringify(largeObject);
const largeObjectBytes = new TextEncoder().encode(largeObjectStringified);
const largeObjectBlob = new Blob([largeObjectBytes], {
type: "application/json;charset=utf-8"
});
const blobUrl = URL.createObjectURL(largeObjectBlob);
//Request body to send to server via axios
let requestBody = new FormData();
let blob = await fetch(blobUrl).then(r => r.blob());
requestBody.append('file', new File([blob], 'file', { type: 'application/json' }));
axios.post(endPoint,requestBody);
Now on my express server I'm reading the incoming blob like this:
let busboy = new Busboy({
headers: req.headers
});
let uploadPath = path.join(__dirname, '..');
uploadPath = path.join(uploadPath, 'uploads/');
let filePaths = [];
busboy.on('file', function (fieldname, file, filename, encoding, mimetype) {
let filePath = path.join(uploadPath, filename);
filePaths.push(filePath);
let writeStream = fs.createWriteStream(filePath);
file.pipe(writeStream);
});
busboy.on('finish', async function () {
let blobFilePath = filePaths[0]; //How do I read back the JSON Object from the file path?
});
Thanks