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

Array concat method in JavaScript

How to make a static http server in nodejs using express

What is 'this.' keyword in JavaScript

How to evaluate a string on a regular expression rule in JavaScript