how to get the client Ip address in app router? #55037
-
how to get the client Ip address in app router? |
Beta Was this translation helpful? Give feedback.
Answered by
leerob
Dec 17, 2023
Replies: 2 comments 6 replies
-
Take a look at https://next-js-playground-dusky.vercel.app/ and https://next-js-playground-dusky.vercel.app/api Option 1 - By Route Handler// route.ts
import { NextRequest, NextResponse } from 'next/server'
export async function GET(req: NextRequest) {
const ip = (req.headers.get('x-forwarded-for') ?? '127.0.0.1').split(',')[0]
return NextResponse.json({ ip })
} Option 2 - By Page Component// page.tsx
import { headers } from 'next/headers'
export default function Home() {
const header = headers()
const ip = (header.get('x-forwarded-for') ?? '127.0.0.1').split(',')[0]
// ...
} Basically, the method is to get Sourcehttps://github.com/devjiwonchoi/next.js-playground/tree/main/app-router Reference |
Beta Was this translation helpful? Give feedback.
1 reply
-
Updated the docs here to make it more clear this depends on the infra provider, but you can also read some things from headers if provided: #59719 |
Beta Was this translation helpful? Give feedback.
5 replies
Answer selected by
leerob
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated the docs here to make it more clear this depends on the infra provider, but you can also read some things from headers if provided: #59719