gpt4 book ai didi

Node.js 和 Protocol Buffer - 如何从帖子中解析 PB

转载 作者:太空宇宙 更新时间:2023-11-03 22:18:57 25 4
gpt4 key购买 nike

我按 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);

});

我对 Node.js 相当陌生,并且还获取发布数据。 req.body 是来自 post 数据的缓冲区吗?

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)

最佳答案

寻找使用 Nodejs 在 http 中进行简单请求响应处理的具体示例。这应该可以帮助您进入解析步骤。 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\n');
req.write('data\n');
req.end();

示例请参阅此答案中请求和响应的解释 https://stackoverflow.com/a/4696338/51700

另一个关于 cookies 的例子 https://stackoverflow.com/a/4581610/51700

关于Node.js 和 Protocol Buffer - 如何从帖子中解析 PB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10733359/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com