Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to handle xhr blob post in nodejs

client code:

var xhr = new XMLHttpRequest();
xhr.open('POST', '/frame', true);
xhr.send(blob);

server code:

app.use(bodyParser.urlencoded({extended: false,limit: '50mb'}));
app.post('/frame', function (req, resp) {
    console.log(req.body);
});

this gives PayloadTooLargeError: too many parameters adding

xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded');

doesn't solve the problem. Any other ideas?

like image 361
Stepan Yakovenko Avatar asked Dec 25 '22 09:12

Stepan Yakovenko


1 Answers

Assuming your blob variable is not really url encoded form data and just any kind of content. Then at the server side you can just read the request stream as it comes in. Remember that the req variable on your http.Server.request event handler is a readable stream. This will remove any size limit imposed by the body-parser middleware. Keeping your original client code then your server code would be:

// app.use(bodyParser.urlencoded({extended: false,limit: '50mb'}));

app.post('/frame', function (req, resp) {
  req.on('readable', function(){
    console.log(req.read());
  });
});

Processing request as it is streamed in is a good idea even for structured data if the content is too big. For example in the past I had hit performance problems when i use body-parser#json middleware with big json requests and solve the issue removing the body-parser#json middleware and using oboe for parsing the streamed input.

like image 117
yeiniel Avatar answered Jan 05 '23 15:01

yeiniel