Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

558
Vistas
Next.js api route with IP address

Inside one of my next.js api route,

For example: pages/api/example.js

I have the following simple code. Then, inside this route, I have post request to a server.

export default async function example(req, res) {
   
  const data = await axios.post(
     https://another_server_Url, // another server api
    {
      data
    })

  res.status(200).json({ name: 'John Doe' })
}

My question is if the another_server api implement a rating-limiting mechanism, It's possible for me to get the user IP address first, then somehow pass the IP address via the axios request. So the another_server api can recognize the true ip.

As far as I understand the current code, the next.js server somehow becomes a proxy.

The problem now is that because I called the Api inside my next router api, the ip address will always be my next.js server.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You may get client's IP from req.headers['x-forwarded-for'] - have a look at: How to determine a user's IP address in node. Then, pass this IP within axios POST request body to another_server.

Edit:

In response to your comment below the implementation largely depends on the configuration of the backend server (another_server) and how it reads the client IP. For example, check if it uses request headers to determine the IP - if so, identify the particular header and overwrite it in axios call to include original client IP, e.g:

  const clientIp = (req.headers['x-forwarded-for'] || '').split(',').pop().trim() || 
    req.socket.remoteAddress;

  const data = await axios({
    method: 'post',
    url: 'https://google.com',
    headers: {
      'x-forwarded-for': clientIp
    },
    // ...
  });

Another solution would be to simply send the request from the client side straight to another_server and skip the problem altogether, but I assume it's not a viable solution in your case.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda