Home > Mobile >  How to save big object to file nodejs?
How to save big object to file nodejs?

Time:12-27

I have a big object that I need to send from server (nodejs) to client.

But every time I try send I get "invalid string length" error. And it's ok, because the object is really big. That's why I'd like to save it to file and then send the file to client.

I don't know the depth of the object. The object itself is and octree.

I don't have any code of saving an object to file, because every time I think about this it leads me to stringify the object, that latter leads to "invalid string length".

Here is a screenshot of the object. Every q(n) key has the same recursive structure as result key.

enter image description here

Thanks!

CodePudding user response:

Firstly try to save in a JSON file and send JSON file directly to client-side

const fs = require('fs');

 fs.writeFileSync("file_name.json", data);
 res.header("Content-Type",'application/json');
 res.sendFile(path.join(__dirname, 'file_name.json'));

CodePudding user response:

A good solution to handling large data transfer between server and client is to stream the data.

Pipe the result to the client like so.

const fs = require('fs');

 const fileStream = fs.createReadStream("path_to_your_file.json");
 res.writeHead(206, {'Content-Type': 'application/json'})
 fileStream.pipe(response)

or follow this blog which stringifies each element at a time and concatenates them.

I will suggest you stream it though.

  • Related