如何在后端使用nodejs + express处理ajax / http-post请求(responsetype:arraybuffer) [英] How to handle ajax/http-post request (responsetype: arraybuffer) using nodejs+express in the backend
问题描述
情况:客户端js向nodejs express服务器发送ajax请求。
Situation : Client js sends ajax request to nodejs express server.
客户端
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数据?
Im stuck at this point. How to access the HTTP POST data?
推荐答案
bodyParser中间件不解析POST二进制数据。当我尝试使用base64编码的字符串时,它将显示为JSON对象中的对象名称,像{data:}之类的东西,显然希望使用形式为name = value的POST数据。
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.
可能有一个处理二进制d的中间件ata,或者您可以通过绑定到数据事件来访问原始数据,并使用 ProtocolBuffers.js wiki 。
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.
这是使用没有快递的香草http模块,但应该工作。
This is using the vanilla http module without express, but should work anyway.
这篇关于如何在后端使用nodejs + express处理ajax / http-post请求(responsetype:arraybuffer)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!