greeter_server.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 grpc = require('@grpc/grpc-js');
  20. var protoLoader = require('@grpc/proto-loader');
  21. var packageDefinition = protoLoader.loadSync(
  22. PROTO_PATH,
  23. {keepCase: true,
  24. longs: String,
  25. enums: String,
  26. defaults: true,
  27. oneofs: true
  28. });
  29. var hello_proto = grpc.loadPackageDefinition(packageDefinition).helloworld;
  30. /**
  31. * Implements the SayHello RPC method.
  32. */
  33. function sayHello(call, callback) {
  34. callback(null, {message: 'Hello ' + call.request.name});
  35. }
  36. /**
  37. * Starts an RPC server that receives requests for the Greeter service at the
  38. * sample server port
  39. */
  40. function main() {
  41. var server = new grpc.Server();
  42. server.addService(hello_proto.Greeter.service, {sayHello: sayHello});
  43. server.bindAsync('0.0.0.0:50051', grpc.ServerCredentials.createInsecure(), () => {
  44. server.start();
  45. });
  46. }
  47. main();