Welcome folks today in this post we will be finding website user public ip address and location
in node.js and javascript using ipware
library. All the full source code of the application will be shown below.
Screenshot
Live Demo
You can see the live demo of application on my website here
Get Started
Now to get started we need to building a new node.js
app by executing the below command
npm init -y
This will create the empty package.json file for your node.js project
And now you need to install the below libraries by using the below command
npm i express
npm i ipware
npm i offline-geo-from-ip
After executing all these commands you just need to make an index.js
file and copy paste the following code
index.js
1 2 3 4 5 6 7 |
var get_ip = require('ipware')().get_ip; app.use(function(req, res, next) { var ip_info = get_ip(req); console.log(ip_info); // { clientIp: '127.0.0.1', clientIpRoutable: false } next(); }); |
So first of all we are importing the ipware
library for getting the ip address
of user and then we are passing this middleware function to every request which is coming to the website and in this we are getting the public ip address
of the user and storing it inside the ip_info variable
Now we will make use of offline-geo-from-ip
library to get further details about the ip address
1 2 3 |
var geoIP = require('offline-geo-from-ip'); console.log(geoIP.allData('199.188.195.120')); |
Now as you can see if you execute this it gets further information of the ip address
such as country,continent,state and city and also the location such as latitude,longitude and time_zone