How to get user's ip address in JavaScript

An IP address is a unique numerical label given to a computer when connected to a network;

ipify.org is a free service that retrieves IP addresses in JSON format

fetch('https://api.ipify.org?format=json')
.then(response => response.json())
.then(response => console.log(JSON.stringify(response)))

// response
{"ip":"--.---.---.---"}

In nodejs you can use a package node-fetch

Install it by typing;

npm i node-fetch

Or using curl, in your terminal type;

curl 'https://api6.ipify.org?format=json'

// response
{"ip":"--.---.---.---"}

Comments

Popular posts from this blog

Redirecting a webpage in JavaScript

What is 'this.' keyword in JavaScript

Introduction to webshare API (navigator.share)