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...
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...
Share Improve this question asked Oct 7, 2015 at 7:43 user4445419user4445419 1- Are you expecting to receive or send a zip file? – filype Commented Oct 12, 2015 at 9:37
1 Answer
Reset to default 5 +50bodyParse.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.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745387398a4625498.html
评论列表(0条)