Node.js和解析协议缓冲区的错误

时间:2012-05-25 09:42:38

标签: node.js protocol-buffers

下面是我尝试从node.js解析协议缓冲区的代码。它在python中工作但在node.js中我似乎遇到了问题。我最关心的是1)我的有效载荷是否正确? 2)我使用协议缓冲区来解析有效负载是否正确。

当我登录到控制台时,有效负载是sys所以我假设是来自请求的prot缓冲区。然后我通过调用:

来解析缓冲区
 bid_request.parse(payload);

以下是代码段和错误:

var buffertools = require('buffertools');
var Schema = require('protobuf').Schema; 
var schema = new Schema(fs.readFileSync('/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/test.desc'));

app.post('/n/test', function(req, res){

      var payload = [];
      req.on('data', function (data) {
        payload.push(data);
      });
      console.log(payload);
      req.on('end', function () {
        var payload = buffertools.concat.apply(null, payload);
        console.log(payload);
        console.log('--------------');
        var bid_request = schema['BidRequest'];

        var brr_fu = bid_request.parse(payload);

        // rest of code here

      });

});


libprotobuf ERROR google/protobuf/message_lite.cc:123] Can't parse message of type "BidRequest" because it is missing required fields: id

undefined:1
urn f.call(self,
      ^
Error: Malformed message
    at Function.parse (unknown source)
    at IncomingMessage.<anonymous> (/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/bidder.js:80:31)
    at IncomingMessage.emit (events.js:64:17)
    at HTTPParser.parserOnMessageComplete [as onMessageComplete] (http.js:130:23)
    at Socket.ondata (http.js:1506:22)
    at TCP.onread (net.js:374:27)

我也很好奇为什么有效载荷是<SlowBuffer>

0 个答案:

没有答案