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

How to make a html5 breakout game in JavaScript

How to tabulate array key values using console.table

How to set content disposition headers for express nodejs apps

Ways of iterating Object properties in JavaScript

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