javascript - Next.js api route with IP address - Stack Overflow

Inside one of my next.js api route,For example: pagesapiexample.jsI have the following simple code.

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 bees 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.

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 bees 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.

Share Improve this question edited Jul 24, 2021 at 22:45 juliomalves 50.6k23 gold badges178 silver badges169 bronze badges asked Jul 24, 2021 at 1:31 YunhaiYunhai 1,4411 gold badge16 silver badges30 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

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 ment 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.',
    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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745156385a4614145.html

相关推荐

  • javascript - Next.js api route with IP address - Stack Overflow

    Inside one of my next.js api route,For example: pagesapiexample.jsI have the following simple code.

    17小时前
    70

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信