Using a parser to transfer a zip file

I have a node application that uses an expression in an application that I need to send via a zip file message (for example, from a postman to a node server), I am currently using a body parser like the following, but I wonder if everything is ok?

app.use(bodyParser.urlencoded({extended: false})); app.use(bodyParser.json()); app.use(bodyParser.text({ type: 'application/text-enriched', limit: '10mb' })); 

Btw this works, but I wonder if I use it correctly ...

+6
source share
1 answer

bodyParse.text() is for body body string . From the documentation:

bodyParser.text (option)

Returns middleware that parses all bodies as a string ...

Since you are loading binary data (like a zip file) using bodyParser.text() , it converts the body of your buffer to utf-8 string . Thus, you will lose some data for binary files, and the zip file may be unreadable.

For a binary file, use bodyParser.raw() , which will give you a buffer in req.body , and you can safely save that buffer to a file.

 app.use(bodyParser.raw({ type: 'application/octet-stream', limit: '10mb' })); 

To download files, you really need to look at multer , which works for multipart/form-data content type.

+2
source

All Articles