얼음꽃의 일지

[JS] 클라이언트 IP 쉽게 가져오기 본문

잡다한 지식

[JS] 클라이언트 IP 쉽게 가져오기

얼음꽃 2023. 11. 11. 20:57
728x90

보통 IP를 가져오게 되면 Header 에 있는

 

x-forward-for 라는 곳에서 찾아가지고 가져오게 되는데요

 

그럼 이럴때마다 저거를 찾아서 적어줘야하기 때문에

 

번거로움이 발생하기 떄문에 쉽게 가져오는 라이브러리를 사용합니다.

 

npm install request-ip --save 설치 후 다음과 같이 사용합니다.

 

const ip = require('request-ip');

exports.a = async(req, res) => {
	const clientIpv6 = req.ip; // 이러면 IPv6 방식;
    const clientIpv4 = req.ip.replacee('::ffff:', ''); // 이러면 IPv4 방식
}

 

을 이용해서 가져와가지고 쉽게 사용 할 수 있습니다.

 

https://www.npmjs.com/package/request-ip

 

request-ip

A small Node.js module to retrieve the request's IP address. Latest version: 3.3.0, last published: a year ago. Start using request-ip in your project by running `npm i request-ip`. There are 646 other projects in the npm registry using request-ip.

www.npmjs.com

 

728x90