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

JavaScript math constants

How to change sound volume using webAudio api gain property

Es6 new features summarized round up

JavaScript intro to classes

How to use canvas context rect method for drawing rectangles