Home > Blockchain >  How to get the client's ip from the request?
How to get the client's ip from the request?

Time:01-03

I want to get the ip addresses of clients when request are being made

But I always get the same result:

::1

How to get exactly the client's ip address, I made requests from different devices, but the result is the same

  @Get(':id')
  async get(@Req() request: Request, @Param('id') id: string) {
    console.log(request.ip);
    return this.adService.findOne({ id });
  }

CodePudding user response:

The request object contains a property called socket, which is actually a net.Socket object. This object has a remoteAddress property which holds the IP address of the call.

request.socket.remoteAddress

Please also note, that if the server is behind a proxy, you should be using the request headers request.headers['x-forwarded-for']

  • Related