Detect user country by IP in Node.js

It shouldn’t be hard to detect a user’s country based on the IP address, right? Right… I really liked the simplicity of ip2location module: load the BIN file, get the location, and close the BIN file.

Prerequisites

Before you install the module, you will need to download the BIN file by registering at ip2location and choosing the appropriate option. You will have a choice between the country, country + city, or other options. Choose the one that suits your needs most, as a bigger database means slower read operations.

Let’s detect that country and city

I chose the option with country, city, zipcode, latitude and longitude. The filename was IP2LOCATION-LITE-DB9.BIN, though it may differ for you.

Now all I need is just 1 module and a path to the BIN file:

npm i --save ip2location-nodejs

Now on to the example. We will prepare an array of IPs to test, and the variable name is… ips of course.

const { IP2Location } = require("ip2location-nodejs");

let ip2location = new IP2Location();
ip2location.open("./IP2LOCATION-LITE-DB9.BIN");

let ips = ['8.8.8.8'];

for (let ip of ips) {
	result = ip2location.getAll(ip);
	for (var key in result) {
		console.log(key + ": " + result[key]);
	}
	console.log("--------------------------------------------------------------");
}

ip2location.close();

You will get a list of information that will look more or less like this:

ip: 8.8.8.8
ipNo: 134744072
countryShort: US
countryLong: United States of America
region: California
city: Mountain View
isp: This method is not applicable for current IP2Location binary data file. Please upgrade your subscription package to install new data file.
domain: This method is not applicable for current IP2Location binary data file. Please upgrade your subscription package to install new data file.
zipCode: 94043
latitude: 37.405991
longitude: -122.078514
timeZone: This method is not applicable for current IP2Location binary data file. Please upgrade your subscription package to install new data file.
netSpeed: This method is not applicable for current IP2Location binary data file. Please upgrade your subscription package to install new data file.
iddCode: This method is not applicable for current IP2Location binary data file. Please upgrade your subscription package to install new data file.
areaCode: This method is not applicable for current IP2Location binary data file. Please upgrade your subscription package to install new data file.
weatherStationCode: This method is not applicable for current IP2Location binary data file. Please upgrade your subscription package to install new data file.
weatherStationName: This method is not applicable for current IP2Location binary data file. Please upgrade your subscription package to install new data file.
mcc: This method is not applicable for current IP2Location binary data file. Please upgrade your subscription package to install new data file.
mnc: This method is not applicable for current IP2Location binary data file. Please upgrade your subscription package to install new data file.
mobileBrand: This method is not applicable for current IP2Location binary data file. Please upgrade your subscription package to install new data file.
elevation: This method is not applicable for current IP2Location binary data file. Please upgrade your subscription package to install new data file.
usageType: This method is not applicable for current IP2Location binary data file. Please upgrade your subscription package to install new data file.
addressType: This method is not applicable for current IP2Location binary data file. Please upgrade your subscription package to install new data file.
category: This method is not applicable for current IP2Location binary data file. Please upgrade your subscription package to install new data file.
--------------------------------------------------------------

Resources


Did you know that I made an in-browser image converter, where you can convert your images without uploading them anywhere? Try it out and let me know what you think. It's free.

Leave a Comment

Your email address will not be published. Required fields are marked *