nodejs尝试msgpack5数据传输方式

官网称要比json更精简,传输更快,今天尝试下

  • server部分, 基于express 4.x, 安装 npm install msgpack5
    res.set("Content-Type", "application/msgpack");
    res.header("Access-Control-Allow-Origin", "*");   
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
     var m = msg.encode({
        a: 1,
        c: 2,
        d: 5
     });
    var lth = req.get("Content-Length");
    console.log(lth);
    req.on("data",function(e){
        console.log(msg.decode(e));
    })
    res.send(m);

前端部分

var oReq = new XMLHttpRequest();    
    oReq.open("POST", "http://localhost:9090", true);    
    oReq.responseType = "arraybuffer";    
var msg = msgpack5();    
    oReq.onload = function (oEvent) {    
var arrayBuffer = oReq.response;     
    if (arrayBuffer) {    
        var byteArray = new Uint8Array(arrayBuffer);    
        var s  = msg.decode(byteArray);    
        console.log(s);    
     }    
    };   
var data =  a.encode(["a","b","c","e"]);   
oReq.send(data);

文章来源: nodejs尝试msgpack5数据传输方式