Git Product home page Git Product logo

ip-location's Introduction

ip-location

Fetch the location of an IP address, host name, or your own location.

Uses freegeoip.net to query for information. freegeoip.net uses MaxMind Geolite 2.

Install

npm i --save ip-location

Usage

With Callbacks

var ipLocation = require('ip-location')

ipLocation('github.com', function (err, data) {
  console.log(data)
})

Outputs:

{ ip: '192.30.252.129',
  country_code: 'US',
  country_name: 'United States',
  region_code: 'CA',
  region_name: 'California',
  city: 'San Francisco',
  zip_code: '94107',
  time_zone: 'America/Los_Angeles',
  latitude: 37.7697,
  longitude: -122.3933,
  metro_code: 807 }

With Promises

var ipLocation = require('ip-location')

ipLocation('github.com')
.then(function (data) {
  console.dir(data)
})
.catch(function (err) {
  console.error(err)
})

Set Your Own Promise Implementation

You can set your own Promise library if you want to use Bluebird or are using Node v0.10.

var ipLocation = require('ip-location')

ipLocation.Promise = require('bluebird')

Use in Browser / or Use Own HTTP Library

If you want to use this in the browser, you must bring your own http GET library to the party. I'd recommend: xhr or fetch.

var ipLocation = require('ip-location')

ipLocation.httpGet = function (url, callback) {
  fetch(url).then(function (resp) {
    return resp.text() // don't use json()
  }).then(function (text) {
    resp.body = text // body should always be set to a string
    callback(null, resp)
  }).catch(function (err) {
    callback(err)
  })
}

Fetch Your Location

Just pass in an empty string.

ipLocation('', function (err, myLocation) {
  console.dir(myLocation)
})

Related

License

MIT - Copyright (c) JP Richardson

ip-location's People

Contributors

jprichardson avatar plepe avatar

Watchers

 avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.