如何在Node.js中处理XHR Blob帖子 [英] how to handle xhr blob post in nodejs
问题描述
客户代码:
var xhr = new XMLHttpRequest();
xhr.open('POST', '/frame', true);
xhr.send(blob);
服务器代码:
app.use(bodyParser.urlencoded({extended: false,limit: '50mb'}));
app.post('/frame', function (req, resp) {
console.log(req.body);
});
这导致PayloadTooLargeError:参数过多 添加
this gives PayloadTooLargeError: too many parameters adding
xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded');
不能解决问题.还有其他想法吗?
doesn't solve the problem. Any other ideas?
推荐答案
假定您的blob变量不是真正的url编码表单数据,而仅仅是任何形式的内容.然后,在服务器端,您可以只读取请求流的传入流.请记住,http.Server.request
事件处理程序上的req
变量是可读的流.这将消除body-parser
中间件施加的任何大小限制.保留原始客户端代码,然后保留服务器代码为:
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());
});
});
处理请求流式传输是一个好主意,即使内容太大,对于结构化数据也是如此.例如,在过去,当我在大型json请求中使用body-parser#json
中间件并解决了删除body-parser#json
中间件并使用 oboe 用于解析流输入.
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.
这篇关于如何在Node.js中处理XHR Blob帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!