1

Earlier I used below code to get the client IP on express.js

req.headers['x-forwarded-for'] ||
req.connection.remoteAddress ||
req.socket.remoteAddress ||
req.connection.socket.remoteAddress

would like to know what is the way to get IP of client with grpc node application. I tried getPeer() which gives always ipv4:127.0.0.1:33944 even when API call is from outside.

Does something equivalent to below go code work?

call.getAttributes().get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR).toString();
Lin Du
  • 88,126
  • 95
  • 281
  • 483

1 Answers1

1

You can get the client IP and port by:

const SayHello = async function (call, callback) {
  const IPandPort = call.getPeer(); // like 127.0.0.1:8080 or just get 127.0.0.1
  console.log(IPandPort);
}

See these issues to get more information:

spike014
  • 459
  • 3
  • 8