using body parser to pass zip file
Asked Answered
E

1

7

I've node app which use express, in the app I need to send via post message zip file (e.g. from postman to the node server) ,currently I use body parser like following but I wonder if this is OK?

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

Btw this is working but I wonder if I use it right...

Earthman answered 7/10, 2015 at 7:43 Comment(1)
Are you expecting to receive or send a zip file?Gennygeno
P
5

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

bodyParser.text(options)

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

Since, you are uploading binary data (e.g. zip file), using bodyParser.text() will convert your buffer body to utf-8 string. So you'll lose some data for binary files and the zip file could be unreadable.

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

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

For file uploads, you should really look at multer, which works for multipart/form-data content-type.

Partridge answered 12/10, 2015 at 11:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.