how to get the ip address of the client from server side in next.js app?

Issue

I want to get the user time zone and location to render a server-side page depending on the location and time zone but I can’t get the user IP address from the request or get the localhost IP address (127.0.0.1). so what should I do ?

Solution

You can use something like request-ip package. Then in your API route:

import requestIp from 'request-ip'

export default async function myRoute(
  req: NextApiRequest,
  res: NextApiResponse
) {
  const detectedIp = requestIp.getClientIp(req)
}

Answered By – Ivan V.

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published