简体   繁体   English

Node.js和协议缓冲区-如何从帖子中解析PB

[英]Node.js and protocol buffers - How to parse a PB from a post

I load by PB schema as follows: 我按PB模式加载如下:

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

Then for a post I expect a pb, I have the following. 然后,对于我希望获得铅的帖子,我有以下内容。

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

        var Feed = schema['MyRequest'];
        var aFeed = Feed.parse(req.body);
        var serialized = Feed.serialize(aFeed);

   });

I am rather new to node.js and also getting post data. 我对node.js相当陌生,并且还获取发布数据。 is the req.body the buffer from the post data? req.body是发布数据中的缓冲区吗?

TypeError: Argument should be a buffer
    at Function.parse (unknown source)
    at /home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/bidder.js:71:22
    at callbacks (/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/node_modules/express/lib/router/index.js:272:11)
    at param (/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/node_modules/express/lib/router/index.js:246:11)
    at pass (/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/node_modules/express/lib/router/index.js:253:5)
    at Router._dispatch (/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/node_modules/express/lib/router/index.js:280:4)
    at Object.handle (/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/node_modules/express/lib/router/index.js:45:10)
    at next (/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/node_modules/express/node_modules/connect/lib/http.js:204:15)
    at Object.methodOverride [as handle] (/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/node_modules/express/node_modules/connect/lib/middleware/methodOverride.js:35:5)
    at next (/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/node_modules/express/node_modules/connect/lib/http.js:204:15)

Looking for specific examples for simple request response handling in http with nodejs. 在使用nodejs的http中寻找用于简单请求响应处理的特定示例。 This should help you get to the parsing step. 这应该可以帮助您进入解析步骤。 http://nodejs.org/api/all.html#all_class_http_serverrequest http://nodejs.org/api/http.html#http_event_data http://nodejs.org/api/all.html#all_class_http_server 请求http://nodejs.org/api/http.html#http_event_data

example: 例:

var options = {
  host: 'www.google.com',
  port: 80,
  path: '/upload',
  method: 'POST'
};

var req = http.request(options, function(res) {
  console.log('STATUS: ' + res.statusCode);
  console.log('HEADERS: ' + JSON.stringify(res.headers));
  res.setEncoding('utf8');
  res.on('data', function (chunk) {
    console.log('BODY: ' + chunk);
  });
});

req.on('error', function(e) {
  console.log('problem with request: ' + e.message);
});

// write data to request body
req.write('data\n');
req.write('data\n');
req.end();

example see explanation of request and response in this answer https://stackoverflow.com/a/4696338/51700 示例请参见此答案中请求和响应的说明https://stackoverflow.com/a/4696338/51700

another example with cookies https://stackoverflow.com/a/4581610/51700 Cookie的另一个示例https://stackoverflow.com/a/4581610/51700

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM