Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
407 views
in Technique[技术] by (71.8m points)

How to implement gRPC Gateway in a node backend

I'm trying to implement gRPC gateway in a Node.JS server following the installation guide.

The steps are writen for golang but I tried to do in Node.JS.

I found this response, and also some packages like this one so is possible implement in node.

After read "insatallation guide", the main problem seems to be get the binary files:

  • protoc-gen-grpc-gateway
  • protoc-gen-openapiv2
  • protoc-gen-go
  • protoc-gen-go-grpc

I have downloaded first two binaries from here so the problem maybe are last two.

I assume go is from golang so I have to search something like protoc-gen-node-grpc. And I've seen this npm package, but, I want to implement by myself as much as is possible. I don't want to depends from third-party people.

At this point I have first to binaries into my path but no the last two.

Once defined gRPC service, next step is generate gRPC stubs. I have this line:

RUN protoc -I=. mygrpc.proto --js_out=import_style=commonjs,binary:./my/folder/ --grpc-web_out=import_style=commonjs,mode=grpcwebtext:./my/folder/

And this generate the files ok. I don't know if I have to use --js_out and --grpc-web_out to create client and service files.

Then, next step is to generate reverse-proxy using protoc-gen-grpc-gateway.

I do (as guide says):

protoc -I=./my/path/ myproto.proto 
  --grpc-gateway_out ./my/path/ 
    --grpc-gateway_opt logtostderr=true 
    --grpc-gateway_opt paths=source_relative 
    --grpc-gateway_opt generate_unbound_methods=true

And this generate a .go file: myproto.pb.gw.go.

Into the file says:

// Code generated by protoc-gen-grpc-gateway. DO NOT EDIT.
// source: myproto.proto

/*
Package myproto is a reverse proxy.

It translates gRPC into RESTful JSON APIs.
*/

So I assume steps are correctly done but: How can I execute this into my Node.JS server?

I have a node project using a Express API, I want to use grpc-gateway instead of express API endpoint... but I only have a .go file.

My proto version is:

libprotoc 3.14.0

Thanks in advance.

question from:https://stackoverflow.com/questions/65617258/how-to-implement-grpc-gateway-in-a-node-backend

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As the issue comment, grpc-gateway only generate Go code. Feel free to use Go, there only need generate code from proto and add service. You can reference my sample code in Java helloworlde/grpc-gateway; And buf is better than protoc to generate code. If you want write all by yourself, you can reference ReflectionCall.java wirte in Java, it can call server without generate code.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...