javascript - using body parser to pass zip file - Stack Overflow

I've node app which use express, in the app I need to send via post message zip file (e.g. from po

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
Add a ment  | 

1 Answer 1

Reset to default 5 +50

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745387398a4625498.html

相关推荐

  • javascript - using body parser to pass zip file - Stack Overflow

    I've node app which use express, in the app I need to send via post message zip file (e.g. from po

    3小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信