Node.js,协议缓冲区和ubuntu - 如何让它工作?

时间:2012-05-24 05:06:21

标签: node.js protocol-buffers

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

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

/home/ubuntu/Downloads/protobuf-2.4.1

2)从此repo下载了这个protobuf-for-node修复程序。我将该文件夹放在node.js app文件夹的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)

2 个答案:

答案 0 :(得分:2)

考虑使用该库的这个分支:

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

似乎更积极地维护并拥有更好的文档。安装它是npm是微不足道的,做require('protobuf')对我来说工作正常。

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

sudo apt-get install libprotobuf-dev protobuf-compiler

看起来你已经拥有了那些但是手动安装它们。

答案 1 :(得分:0)

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

buftest.proto

package com.chrisdew.buftest;

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

buftest.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>