greeter_client.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. *
  3. * Copyright 2015 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. var PROTO_PATH = __dirname + '/../../protos/helloworld.proto';
  19. var parseArgs = require('minimist');
  20. var grpc = require('@grpc/grpc-js');
  21. var protoLoader = require('@grpc/proto-loader');
  22. var packageDefinition = protoLoader.loadSync(
  23. PROTO_PATH,
  24. {keepCase: true,
  25. longs: String,
  26. enums: String,
  27. defaults: true,
  28. oneofs: true
  29. });
  30. var hello_proto = grpc.loadPackageDefinition(packageDefinition).helloworld;
  31. function main() {
  32. var argv = parseArgs(process.argv.slice(2), {
  33. string: 'target'
  34. });
  35. var target;
  36. if (argv.target) {
  37. target = argv.target;
  38. } else {
  39. target = 'localhost:50051';
  40. }
  41. var client = new hello_proto.Greeter(target,
  42. grpc.credentials.createInsecure());
  43. var user;
  44. if (argv._.length > 0) {
  45. user = argv._[0];
  46. } else {
  47. user = 'world';
  48. }
  49. client.sayHello({name: user}, function(err, response) {
  50. console.log('Greeting:', response.message);
  51. });
  52. }
  53. main();