1+ /*-----------------------------------------------------------------------
2+ THIS SERVER IS USED FOR DEFINING LOGIC TO THE FUNCTIONS INSIDE PROTO FILE
3+ AND DECIDES WHAT FUNCTION DOES CLIENT GETS TO USE
4+ -------------------------------------------------------------------------*/
5+
6+ // Importing the proto file needed to use for this service
17const PROTO_PATH = __dirname + "/protos/employee.proto" ;
28
9+ // Require packages
310const grpc = require ( "grpc" ) ;
411const protoLoader = require ( "@grpc/proto-loader" ) ;
512const _ = require ( "lodash" ) ;
613
14+ // PackageDefinition used for generating code from the proto file using protocal buffer compiler
715const packageDefinition = protoLoader . loadSync ( PROTO_PATH , {
816 keepCase : true ,
917 longs : String ,
@@ -12,21 +20,30 @@ const packageDefinition = protoLoader.loadSync(PROTO_PATH, {
1220 oneofs : true ,
1321} ) ;
1422
23+ // defining the employee proto from the proto file
24+ // the "package employee;" we used in proto file comes here and loads all the service and messages inside
1525let employeeProto = grpc . loadPackageDefinition ( packageDefinition ) . employee ;
1626
27+ // dunmmy data for interaction
1728const { employees } = require ( "./dummyEmp" ) ;
1829
30+ // the function getDetails we defined in employee proto
1931function getDetails ( call , callback ) {
2032 callback ( null , {
2133 message : _ . find ( employees , { id : call . request . id } ) ,
2234 } ) ;
2335}
2436
37+ // running the actual server
2538function main ( ) {
26- const server = new grpc . Server ( ) ;
39+ const server = new grpc . Server ( ) ; // instantiate a grpc server
40+
41+ // Adding the service to server and declaring the user-defined function to the proto function
42+ // getDetails is given to let the client know that it can use this function
2743 server . addService ( employeeProto . Employee . service , { getDetails : getDetails } ) ;
28- server . bind ( "0.0.0.0:3000" , grpc . ServerCredentials . createInsecure ( ) ) ;
29- console . log ( "RUNNING ON 3000" ) ;
44+
45+ server . bind ( "0.0.0.0:3001" , grpc . ServerCredentials . createInsecure ( ) ) ; // Binds to a port -- createInsecure() is given to say there is no authentication in the server
46+ console . log ( "RUNNING ON 3001" ) ;
3047 server . start ( ) ;
3148}
3249
0 commit comments