This is a REST API built using node.js version 8.11.2, express framework and MySQL database, which fetches user's public IP address and finds his (2 character)country code, country name, state/region and city name and returns result in JSON format.
Steps to install
-
You can clone using command
git clone https://github.com/aratidgr8/iptocountry.git;
or
Download iptocountry-master.zip file. -
Navigate to folder in terminal or command line interface.
-
To install all node modules and their dependencies required in code, from package.json file, run below command:
npm install
-
Unzip csv-databases.zip folder.
-
Move IP2LOCATION-LITE-DB3.CSV and IP2LOCATION-LITE-DB3.IPV6.CSV files from csv-databases directory to root folder (same folder, where csv2sql.php file is present).
-
Create database ip2location
CREATE DATABASE ip2location;
-
Create table ip2location_db3 which stores; IPV4, country code, country name, state and city name information.
CREATE TABLE
ip2location_db3
(ip_from
INT(10) UNSIGNED,ip_to
INT(10) UNSIGNED,country_code
CHAR(2),country_name
VARCHAR(64),region_name
VARCHAR(128),city_name
VARCHAR(128), INDEXidx_ip_from
(ip_from
),INDEXidx_ip_to
(ip_to
),INDEXidx_ip_from_to
(ip_from
,ip_to
)) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin; -
Create table ip2location_db3_ipv6 which stores; IPV6, country code, country name, state and city name information.
CREATE TABLE
ip2location_db3_ipv6
(ip_from
DECIMAL(39,0) UNSIGNED NULL DEFAULT NULL,ip_to
DECIMAL(39,0) UNSIGNED NOT NULL,country_code
CHAR(2),country_name
VARCHAR(64),region_name
VARCHAR(128),city_name
VARCHAR(128), INDEXidx_ip_from
(ip_from
), INDEXidx_ip_to
(ip_to
), INDEXidx_ip_from_to
(ip_from
,ip_to
)) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin; -
Open csv2sql.php file on your localhost and submit form to import data from CSV file IP2LOCATION-LITE-DB3.CSV to Mysql table: ip2location_db3.
It should load table ip2location_db3 with 3278000 rows. -
In csv2sql.php file now change the "table name" input to ip2location_db3_ipv6 and "Name of the file" input to IP2LOCATION-LITE-DB3.IPV6.CSV and submit the form once again.
It should load table ip2location_db3_ipv6 with 3719942 rows. -
Go to terminal and run below command:
node app.js
-
Now, go to the browser and load below URL:
http://localhost:4001/api/v1/userscountry
-
Script fetched your IP address and returns your location information in JSON format as below:
{"country_code":"IN","country_name":"India","region_name":"Gujarat","city_name":"Chanasma"}