English 中文(简体)
• 如何让客户在接下来的13岁时住进去?
原标题:How to get client s ip address in Next.js 13?

下面是第13点,希望加上APIC末点代码<>/api/getIP。

route.js file is in the /app/api/getIP/route.js.

我尝试了几种方式,但都没有奏效。 我尝试使用<条码>request-ip 来登上正本,但返回<代码>null<>。

Here is my code:

import { NextResponse } from "next/server";
import requestIp from "request-ip";

export async function GET(req) {
  const detectedIp = requestIp.getClientIp(req);
  const data = {
    ok: true,
    ip: detectedIp,
    userAgent: req.headers.get("user-agent"),
  };
  return new NextResponse(JSON.stringify(data, null, 2));
}

我在访问时收到这一答复:

{
    "ok": true,
    "ip": null,
    "userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36"
}
问题回答




相关问题
How to get IP address from sockaddr

I want to try and get the ip address of a client after calling accept. This is what I have so far, but I just end up getting some long number that is clearly not an ip address. What could be wrong? ...

printing ip addresses using gdb

I am debugging a networking code and want to print ip addresses which are declared as int32. when i print it using gdb print command, i get some values which is not much meaningful. How can i ...

Using Clients IP as the Servers IP

This might be one of those "huh, why?" questions, but I figured it would be worth the try. How would one, from a server-side application, use the clients IP address as the applications IP address to ...

where is class E ip addresses?

I am reading Richard Stevens TCP/IP illustrated Vol1.He said class E ip addresses are reserved for future use.Considering he wrote the book at 1994,now is the future.So where is the class E ip ...

multicast ip address - blocked in call to recvfrom

i am writing a simple multicast application. i intend to run it on localhost. i have done the following: char *maddr; . . . sendfd = socket(...); struct sockaddr_in sasend; sasend.sin_family = ...

热门标签