# ipident

> Node.js library to identify the location of IP addresses using redis.

Latest version **0.0.7** (published 2013-05-25) · 0 weekly downloads

## Install

```sh
npm install ipident
pnpm add ipident
yarn add ipident
bun add ipident
```

## Health

**Score 10/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; low quality score; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.7 |
| Published | 2013-05-25 |
| First published | 2013-04-14 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Node | * |
| Dependencies | 6 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | T. Budiman |
| Maintainers | tbudiman |

## Links

- npm: https://www.npmjs.com/package/ipident
- Repository: http://github.com/Webizly/geoip
- npm.io page: https://npm.io/package/ipident

## Dependencies (6)

- [csv](https://npm.io/package/csv.md) *
- [inet](https://npm.io/package/inet.md) *
- [async](https://npm.io/package/async.md) *
- [redis](https://npm.io/package/redis.md) ~0.8.2
- [hiredis](https://npm.io/package/hiredis.md) *
- [underscore.string](https://npm.io/package/underscore.string.md) *

## Recent versions

- 0.0.7 (latest) — 2013-05-25
- 0.0.6 — 2013-04-20
- 0.0.5 — 2013-04-17
- 0.0.4 — 2013-04-14
- 0.0.3 — 2013-04-14
- 0.0.2 — 2013-04-14

## README

ipident - IP identification lookup using redis
==============================================

This module provides a simple Node.js API interface to identify city information from a given IP Address.
The module will setup a redis data store and load the IP lookup data there.

Installation
------------

You have to install redis and node.js in order to use this module.

Install with:

```sh
$ npm install ipident
```

Or get it from the source: [ipident]

The IP lookup data is available at [geolite]. Download the GeoLite City data in CSV format.
Extract them into `node_modules/ipident/data/` directory. Note that there should be 2 files:

```
node_modules/ipident/data/GeoLiteCity-Blocks.csv
node_modules/ipident/data/GeoLiteCity-Location.csv
```

Usage
-----

```javascript
var ipident = require('ipident');

function doLookup() {
    ipident.retrieveCityInfo('125.163.49.39', function (data) {
        console.log(data);
    });
}

// loads data into redis
ipident.autoLoad(doLookup);

```

API
---

### loadData(callback)

Read the data from csv file and load them to redis data store. This application only use 1 key, `ipident:ipaddress`.
Usually this function isn't used directly, use `autoLoad` instead.

### autoLoad(callback)

Check the data in redis and load them if necessary. This function is used in starting up the application to ensure the data is ready.

```javascript
var ipident = require('ipident');

ipident.autoLoad();
```

### retrieveCityInfo(ip_address, callback(data))

This is the main function to lookup city information. The returned data is an object with the following fields:
 * city_name
 * region_name
 * country_code
 * postal_code
 * latitude
 * longitude
 * metro_code
 * area_code

### clearData(callback)

Deletes all data from redis

### countData(callback)

Count all data from redis

Sample Web Application using `ipident`
--------------------------------------

Sample of usage inside a web application using express framework. The web application will prepare the data and load it to redis. On browser request, the remote ip of the requester will be identified, and by using `ipident.retrieveCityInfo` it will find the city and displays it in the browser.

Install express
```sh
$ npm install express
```

server.js:

```javascript
var express = require('express'),
    ipident = require('ipident');
var app = express();

ipident.autoLoad();

app.get('/', function(req, res) {
    var body = 'IP Address: ' + req.ip + "\n",
        start = process.hrtime();

    ipident.retrieveCityInfo(req.ip, function (data) {

        if (data) {
            var city_info, diff;

            city_info = "City: " + data.city_name + "\n";
            city_info += "Region Name: " + data.region_name + "\n";
            city_info += "Country: " + data.country_code + "\n";
            city_info += "Postal Code: " + data.postal_code + "\n";
            city_info += "Latitude: " + data.latitude + "\n";
            city_info += "Longitude: " + data.longitude + "\n";
            city_info += "Metro Code: " + data.metro_code + "\n";
            city_info += "Area Code: " + data.area_code + "\n";
            body += city_info;
            diff = process.hrtime(start);
            body += "\n\nIdentification process took " +
                (diff[0] * 1e9 + diff[1]).toString() +
                " nanoseconds\n";
            res.setHeader('Content-Type', 'text/plain');
            res.setHeader('Content-Length', body.length);
            res.end(body);
        } else {
            body += "No location identified for your IP Address.\n";
            res.setHeader('Content-Type', 'text/plain');
            res.setHeader('Content-Length', body.length);
            res.end(body);
        }
    });
});

app.get(/\/ipv4\/(\d+\.\d+\.\d+\.\d+)/, function(req, res) {
    var given_ip = req.params[0];
    var start = process.hrtime();
    var body = 'Lookup IP Address: ' + given_ip + "\n";
    ipident.retrieveCityInfo(given_ip, function (data) {

        if (data) {
            var city_info, diff;

            city_info = "City: " + data.city_name + "\n";
            city_info += "Region Name: " + data.region_name + "\n";
            city_info += "Country: " + data.country_code + "\n";
            city_info += "Postal Code: " + data.postal_code + "\n";
            city_info += "Latitude: " + data.latitude + "\n";
            city_info += "Longitude: " + data.longitude + "\n";
            city_info += "Metro Code: " + data.metro_code + "\n";
            city_info += "Area Code: " + data.area_code + "\n";
            body += city_info;
            diff = process.hrtime(start);
            body += "\n\nIdentification process took " +
                (diff[0] * 1e9 + diff[1]).toString() +
                " nanoseconds\n";
            res.setHeader('Content-Type', 'text/plain');
            res.setHeader('Content-Length', body.length);
            res.end(body);
        } else {
            body += "No location identified for your IP Address.\n";
            res.setHeader('Content-Type', 'text/plain');
            res.setHeader('Content-Length', body.length);
            res.end(body);
        }
    });

});

app.listen(3001);
console.log('Listening on port 3001');

```

  [ipident]: https://github.com/Webizly/geoip/
  [geolite]: http://dev.maxmind.com/geoip/geolite

---
_Source: https://npm.io/package/ipident · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
