How to handle ajax/http-post request (responsetype: arraybuffer) using nodejs+express in the backend

假如想象 提交于 2020-02-15 22:52:15

问题


Situation : Client js sends ajax request to nodejs express server.

Client

xmlHttpRequest=new XMLHttpRequest();  
xmlHttpRequest.open("POST","/some/server/path,true);
xmlHttpRequest.responseType="arraybuffer";
xmlHttpRequest.send(new Uint8Array(arraybufferobject));

Server(so far)

var express = require('express');
var server = express();
server.use(express.static(__dirname));
server.use(express.bodyParser());
server.post('/goforms/modbus/',function(req,res,next){
    //How to access the uint8array || arraybuffer ?
});

server.listen(80);

Im stuck at this point. How to access the HTTP POST data?


回答1:


The bodyParser middleware does not parse POSTed binary data. When i tried base64 encoded strings, it would show up as the object name in a JSON object, something like {"data":}, obviously expecting POST-data in the form name=value.

There might be a middleware that deals with binary data, or you can access the raw data by binding to the "data" event and stack the received chunks into a buffer using the method described in the ProtocolBuffers.js wiki.

This is using the vanilla http module without express, but should work anyway.




回答2:


I don't know about arraybuffer but usually, we can access the POST data using the req.body parameter. Does that work for you?



来源:https://stackoverflow.com/questions/21573496/how-to-handle-ajax-http-post-request-responsetype-arraybuffer-using-nodejsex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!