Last active
February 15, 2021 14:44
-
-
Save stenito/b29adff40770f6e08caae2c15d957915 to your computer and use it in GitHub Desktop.
Get public IP address (wan IP address) in node
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const https = require('https'); | |
const options = { | |
hostname: 'httpbin.org', | |
port: 443, | |
path: '/ip', | |
method: 'GET' | |
} | |
const req = https.request(options, res => { | |
res.on('data', d => { | |
let yourWanIP = JSON.parse(d).origin; | |
// do what you want to do with the IP address | |
// ... eg. log it to the console | |
console.log(yourWanIP); | |
}); | |
}) | |
req.on('error', error => { | |
console.error(error); | |
}) | |
req.end(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment