源代码位置
1. 下载官方示例
git clone git@github.com:grpc/grpc-node.git
- 复制
examples\helloworld\static_codegen
demo
2. 得到如下目录
npm i @grpc/grpc-js
npm i google-protobuf
npm i minimist
2.1 安装工具依赖
npm install -g grpc-tools
2.2 编译 .proto
文件
cd ./pb
grpc_tools_node_protoc.cmd --js_out=import_style=commonjs,binary:./ --grpc_out=grpc_js:./ helloworld.proto
3. 修改官方示例
// greeter_server.js
- server.addService(services.GreeterService, {sayHello: sayHello});
+ server.addService(services['helloworld.Greeter'], {sayHello: sayHello});
// greeter_client.js
- var client = new services.GreeterClient(target,
grpc.credentials.createInsecure());
+ var GreeterClient = grpc.makeGenericClientConstructor(services['helloworld.Greeter']);
+ const client = new GreeterClient(target, grpc.credentials.createInsecure())
4.运行
node .\greeter_server.js
node .\greeter_client.js