Created
December 31, 2019 04:55
-
-
Save gistlyn/05701a72bf35524fb897b81c1bac767b to your computer and use it in GitHub Desktop.
Node.js Google protoc insecure GrpcServicesClient TodoWorld Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { Hello } = require('./services_pb.js'); | |
const { GrpcServicesClient } = require('./services_grpc_pb.js'); | |
const grpc = require('grpc'); | |
const { promisify } = require('util'); | |
async function main() { | |
const client = new GrpcServicesClient('todoworld.servicestack.net:5054', | |
grpc.credentials.createInsecure()); | |
// Convert gRPC's callback APIs to await friendly promises | |
const { getHello } = promisifyAll(client); | |
let request = new Hello(); | |
request.setName("gRPC Node.js"); | |
let response = await getHello(request); | |
console.log(response.getResult()); | |
} | |
function promisifyAll(client) { | |
const to = {}; | |
for (var k in client) { | |
if (typeof client[k] != 'function') continue; | |
to[k] = promisify(client[k].bind(client)); | |
} | |
return to; | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment