Welcome aboard! We are happy you are here and wish you good net-raft!
// at first you install "npm install ip" in node.js command prompt
//put this code into getip.js
var http = require('http');
var ip = require('ip');
var srv = http.createServer(function (req, res) {
var address = ip.address();
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(address);
});
srv.listen(3000);
console.log("Listening to Port 3000");
// type "node getip.js" in command prompt
// then put this url "http://localhost:3000/address" into any browser
// this node.js code put into app.js
const request = require('request')
request('http://ipinfo.io', function(error, res, body) {
var ipuser = JSON.parse(body)
console.log(ipuser.ip)
})
// then type "node app.js" in command prompt
// this is a functional solution, copy and use
//put this code into getip.js
var http = require('http');
const request = require('request')
var srv = http.createServer(function (req, res) {
request('http://ipinfo.io', function(error, res1, body) {
var ipuser = JSON.parse(body)
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(ipuser.ip);
})
});
srv.listen(3000);
console.log("Listening to Port 3000");
// type "node getip.js" in command prompt
// then put this url "http://localhost:3000/" into any browser
// type "node script.js" in node.js command prompt
// place into script.js
var os = require('os');
var interfaces = os.networkInterfaces();
var addresses = [];
for (var k in interfaces) {
for (var k2 in interfaces[k]) {
var address = interfaces[k][k2];
if (address.family === 'IPv4' && !address.internal) {
addresses.push(address.address);
}
}
}
console.log(addresses);
The most helpful NODEJS solutions