English 中文(简体)
node.js 和协议缓冲 - 如何从 pos 上解析 PB 数据
原标题:Node.js and protocol buffers - How to parse a PB from a post

我用PB的图案装载如下:

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

然后,对于一个职位,我期望一个pb,我有以下内容。

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

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

   });

我比较新才才刚到结节 Js, 并获得邮资数据。 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. 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

例如:

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
 );
req.write( data
 );
req.end();

example see explanation of request and response in this answer https://stackoverflow.com/a/4696338/51700

another example with cookies https://stackoverflow.com/a/4581610/51700





相关问题
Serializing a List of objects using Protobuf-net

I ve been looking to do some binary serialization to file and protobuf-net seems like a well-performing alternative. I m a bit stuck in getting started though. Since I want to decouple the definition ...

How to use protocol buffers?

Could someone please help and tell me how to use protocol buffers. Actually I want to exchange data through sockets between a program running on unix and anoother running on windows in order to run ...

Using Protocol buffer as general Data object?

We re introducing protocol buffers as the new transport for some back end RPC services. Because there s resistance to manually shuttling data between different forms of similar objects, I can forsee ...

Go integration with Protocol Buffers?

After a quick look at the documentation, I immediately started to think about integration with existing languages and applications and was wondering whether support would be provided for Protocol ...

Android and Protocol Buffers

I am writing an Android application that would both store data and communicate with a server using protocol buffers. However, the stock implementation of protocol buffers compiled with the LITE flag (...

热门标签