3

以下是我用来尝试在 node.js 中加载 proto 文件的步骤。总的来说,我如何让协议缓冲区与 node.js 一起工作?

1) 将协议缓冲区下载并安装到此位置。

/home/ubuntu/Downloads/protobuf-2.4.1

2) 从这个 repo 下载了这个 protobuf-for-node 修复。我将该文件夹放在我的 node.js 应用程序文件夹的 node_modules 文件夹中。

git://github.com/pzgz/protobuf-for-node.git

3)运行以下命令

cd node_modules/protobuf-for-node
PROTOBUF=~/Downloads/protobuf-2.4.1 /usr/local/bin/node-waf configure clean build

4)将以下文件移动到这些位置:

cp node_modules/protobuf-for-node/protobuf_for_node.h /usr/local/include/node
cp node_modules/protobuf-for-node/build/Release/protobuf_for_node_lib.so /usr/local/lib

5) 运行以下命令来构建 node.js 附带的 feeds.proto 示例

cd node_modules/protobuf-for-node/example
protoc --descriptor_set_out=feeds.desc --include_imports feeds.proto

6)在应用程序中,我添加了这一行。

var express = require('express'), routes = require('./routes');
var fs = require('fs');
var puts = require('util').puts;
url = require('url');
moment = require('moment');
var uuid = require('node-uuid');
var app = module.exports = express.createServer();
var Schema = require('protobuf_for_node').Schema;
var schema = new Schema(fs.readFileSync('/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/node_modules/protobuf-for-node/example/feeds.desc'));

7)这是我的错误。

module.js:337
    throw new Error("Cannot find module '" + request + "'");
          ^
Error: Cannot find module 'protobuf_for_node'
    at Function._resolveFilename (module.js:337:11)
    at Function._load (module.js:279:25)
    at Module.require (module.js:359:17)
    at require (module.js:375:17)
    at Object.<anonymous> (/home/ubuntu/app.js:9:14)
    at Module._compile (module.js:446:26)
    at Object..js (module.js:464:10)
    at Module.load (module.js:353:31)
    at Function._load (module.js:311:12)
    at Array.0 (module.js:484:10)
4

2 回答 2

2

考虑改用这个库的分支:

https://github.com/chrisdew/protobuf/

它似乎得到了更积极的维护并且有更好的文档。安装它是 npm 是微不足道的,做一个require('protobuf')对我来说很好。

对于 Ubuntu,我需要安装以下内容以使用 npm 安装软件包并编译 proto 文件:

sudo apt-get install libprotobuf-dev protobuf-compiler

看起来您已经有了这些,但手动安装了它们。

于 2012-05-29T15:36:56.003 回答
0

这是我为https://github.com/chrisdew/protobuf/issues/29做的一个例子——大多数用户不需要复杂的字节字段。

自助测试.proto

package com.chrisdew.buftest;

message BufTest {
  optional float num  = 1;
  optional bytes payload = 2;
}

自助测试.js

var fs = require('fs');
var Schema = require('protobuf').Schema;

// "schema" contains all message types defined in buftest.proto|desc.
var schema = new Schema(fs.readFileSync('buftest.desc'));

// The "BufTest" message.
var BufTest = schema['com.chrisdew.buftest.BufTest'];

var ob = { num: 42 };
ob.payload = new Buffer("Hello World");

var proto = BufTest.serialize(ob);
console.log('proto.length:', proto.length);

var outOb = BufTest.parse(proto);
console.log('unserialised:', JSON.stringify(outOb));

var payload = new Buffer(outOb.payload);
console.log(payload);

Makefile:(第二行以 TAB 开头,而不是空格)

all:
    protoc --descriptor_set_out=buftest.desc --include_imports buftest.proto

输出:

$ node buftest.js 
proto.length: 18
unserialised: {"num":42,"payload":{"0":72,"1":101,"2":108,"3":108,"4":111,"5":32,"6":87,"7":111,"8":114,"9":108,"10":100,"length":11}}

payload: <Buffer 48 65 6c 6c 6f 20 57 6f 72 6c 64>
于 2013-09-05T13:15:25.603 回答