如何在后端使用nodejs + express处理ajax / http-post请求(responsetype:arraybuffer)

时间:2014-02-05 09:40:41

标签: ajax node.js express http-post arraybuffer

情况:客户端js向nodejs express服务器发送ajax请求。

客户

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

服务器(到目前为止)

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);

我陷入了困境。如何访问HTTP POST数据?

2 个答案:

答案 0 :(得分:1)

bodyParser中间件不解析POSTed二进制数据。当我尝试使用base64编码的字符串时,它会在JSON对象中显示为对象名称,类似于{“data”:},显然期望以name = value形式显示POST数据。

可能存在处理二进制数据的中间件,或者您可以通过绑定到“data”事件来访问原始数据,并使用ProtocolBuffers.js wiki中描述的方法将接收到的块堆叠到缓冲区中。 / p>

这是使用没有快递的vanilla http模块,但无论如何都应该工作。

答案 1 :(得分:0)

我不了解arraybuffer,但通常我们可以使用req.body参数访问POST数据。这对你有用吗?