Skip to content

Instantly share code, notes, and snippets.

@stenito
Last active February 15, 2021 14:44
Show Gist options
  • Save stenito/b29adff40770f6e08caae2c15d957915 to your computer and use it in GitHub Desktop.
Save stenito/b29adff40770f6e08caae2c15d957915 to your computer and use it in GitHub Desktop.
Get public IP address (wan IP address) in node
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