与节点 js 一起使用的 GRPC 客户端面临问题

GRPC client using with node js facing issue

我在尝试使用节点 js 创建 grpc 客户端调用时遇到问题。当我在原型文件中使用 import "google/api/annotations.proto" 时,出现以下错误。如果我删除它,它就可以工作。我可以知道我的 client.js

中缺少什么吗

Error: unresolvable extensions: 'extend google.protobuf.MethodOptions' in .google.api at Root.resolveAll (src/github.com/workspace/explorer/node_modules/protobufjs/src/root.js:255:15) at Object.loadSync (/src/github.com/workspace/explorer/node_modules/@grpc/proto-loader/build/src/index.js:224:16) at Object. (/src/github.com/workspace/explorer/server/grpc/client.js:3:37)

syntax = 'proto3';

import "google/api/annotations.proto";
import "google/protobuf/timestamp.proto";

package chain;

service chain {
  
  rpc GetHeight(HeightRequest) returns(HeightResponse) { option (google.api.http).get = "/api/height/{height}";}
 
}

message HeightRequest {
  string hash = 1;
}

message HeightResponse {
  int64 height=1;
}


client.js

var PROTO_PATH = __dirname + '/proto/chain.proto';
var parseArgs = require('minimist');
var grpc = require('@grpc/grpc-js');
var protoLoader = require('@grpc/proto-loader');
var packageDefinition = protoLoader.loadSync(
    PROTO_PATH,
    {
        keepCase: true,
        longs: String,
        enums: String,
        defaults: true,
        oneofs: true,

    });
var chain_proto = grpc.loadPackageDefinition(packageDefinition).chain;

function main() {
    var argv = parseArgs(process.argv.slice(2), {
        string: 'target'
    });
    var target;
    if (argv.target) {
        target = argv.target;
    } else {
        target = 'localhost:9040';
    }
    var client = new chain_proto.chain(target,
        grpc.credentials.createInsecure());

    client.GetHeight(function (err, response) {
        console.log('height:', response);
    });
}

main();

我找到了上述错误的解决方案,您需要在项目目录中创建一个文件夹 googleapis->google->api 然后需要从 grpc-gateway GitHub 添加一个 annotation.proto 文件,例如在此提及 link Grpc-gateway

接下来需要添加路径如下图

 PROTO_PATH,
    {
        keepCase: true,
        longs: String,
        enums: String,
        defaults: true,
        oneofs: true,
        includeDirs: [
            __dirname + '/googleapis',
        ]

    });