Node JS 使用自定义 headers 调用 GRPC 服务器

Node JS Call GRPC Server with custom headers

我正在尝试调用 GRPC 端点,但我想提供客户身份验证 header。我在哪里指定这个?

var client = new proto.Publisher('127.0.0.1:50051',
    grpc.credentials.createInsecure());

var customHeader = { 
    'authorization': 'secret'
}

client.publish(data, function (err, response) {
  console.log('Sent');
});

您需要创建一个 grpc.Metadata 对象,然后将其作为可选参数传递给方法:

var client = new proto.Publisher('127.0.0.1:50051',
    grpc.credentials.createInsecure());

var metadata = new grpc.Metadata();
metadata.add('authorization', 'secret')

client.publish(data, metadata, function (err, response) {
  console.log('Sent');
});