Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

hapi-geo-locate

futurestudio528MIT4.1.1TypeScript support: included

Provide IP geo location for incoming requests in hapi

IP location, geo, geo-location, hapi, hapijs, ipLocation, location, plugin

readme

hapi-geo-locate logo

A hapi plugin to geo-locate requests


Installation · Usage · Plugin Options · Proxies and Headers



Build Status Known Vulnerabilities Latest Version Total Downloads

Follow @marcuspoehls for updates!


Development of this hapi plugin is supported by Future Studio University 🚀
Join the Future Studio University and Skyrocket in Node.js


Introduction

A hapi plugin to geo locate requests by IP and provide request.location in your route handlers. The plugin uses ipinfo.io for the IP geo location.

Requirements

hapi v19 (or later) and Node.js v12 (or newer)

This plugin requires hapi v19 (or later) and Node.js v12 or newer.

Compatibility

Major Release hapi.js version Node.js version
v4 >=17 hapi >=12
v3 >=17 hapi >=8
v2 <=16 hapi >=8

Installation

Add hapi-geo-locate as a dependency to your project:

npm i hapi-geo-locate

Using hapi v17 or v18?

Use the 3.x release line:

npm i hapi-geo-locate@3

Do you use hapi v16 (or lower)?

Use the 2.x release of hapi-geo-locate with hapi v16 support. Later versions are only compatible with hapi v17 and v18.

npm i hapi-geo-locate@2

Usage

The most straight forward way to register the hapi-geo-locate plugin:

await server.register({
    plugin: require('hapi-geo-locate')
})

// went smooth like dark chocolate :)

// Within your route handler functions, you can access the location like this
server.route({
    method: 'GET',
    path: '/',
    handler: (request, h) => {
        const location = request.location

        // use client location

        return location
    }
})

Plugin Registration Options

The following plugin options allow you to customize the default behavior of hapi-geo-locate:

  • enabledByDefault: (boolean), default: true — by default, the plugin geo locates the request by IP on every request
  • authToken: (string), no default — the API token to authenticate requests against the IPinfo API
await server.register({
  plugin: require('hapi-geo-locate'),
  options: {
    enabledByDefault: false
    authToken: 'your-ipinfo-api-token'
  }
})

// Within your route handler functions, you can access the location like this
server.route({
  method: 'GET',
  path: '/',
  handler: (request, h) => {
    const location = request.location // will be undefined

    return h.response(location)
  }
})

Route Handler Options

The following plugin options on individual route handlers allow you to customize the behavior of hapi-geo-locate:

  • enabled: (boolean) — tells the plugin to enable (true) or disable (false) geo location for the request by IP
  • fakeIP: (string) — tells the plugin to use the defined IP address to geo locate the request (by this IP)

The plugin configuration can be customized for single routes using the hapi-geo-locate key:

server.register({
  plugin: require('hapi-geo-locate') // enabled by default
})

// Within your route handler functions, you can access the location like this
server.route({
  method: 'GET',
  path: '/',
  handler: (request, h) => {
    const location = request.location
    // use the location

    return location
  },
  config: {
    plugins: {
      'hapi-geo-locate': {
        enabled: true,
        fakeIP: '8.8.8.8'
      }
    }
  }
})

Supported Proxies and Proxy Headers

hapi-geo-locate supports all proxies that request-ip does:

  • X-Client-IP
  • X-Forwarded-For, picking the first, client IP if the request went through multiple proxies.
  • X-Forwarded, Forwarded-For and Forwarded as variations of X-Forwarded-For
  • CF-Connecting-IP
  • True-Client-Ip
  • X-Real-IP
  • X-Cluster-Client-IP
  • and all the request.[connection|socket|info].remoteAddress variations.

If the IP address cannot be found, null is returned.

Running your application behind a (reverse) proxy like nginx, the client’s IP address gets reset to localhost. You can grab the actual request IP to your app using an HTTP header.

hapi-geo-locate uses the request-ip package to determine the external IP address. This package supports all common HTTP headers and ways to get the request’s IP. Awesome!

You should be safe in any way :)

Feature Requests

Do you miss a feature? Please don’t hesitate to create an issue with a short description of your desired addition to this plugin.

Contributing

  1. Create a fork
  2. Create your feature branch: git checkout -b my-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request 🚀

License

MIT © Future Studio


futurestud.io  ·  GitHub @futurestudio  ·  Twitter @futurestud_io

changelog

Changelog

Version 4.1.1 - 2020-08-05

Updated

  • bump dependencies
  • test against Node.js v14
  • replaced request-ip dependency with @supercharge/request-ip improving request IP detection

Version 4.1.0 - 2020-04-05

Added

  • new plugin option: authToken
    • the authToken option represents the IPinfo API token to authenticate requests

Updated

  • bump dependencies

Version 4.0.0 - 2020-01-10

Breaking Changes

  • require Node.js v12
    • this change aligns with the hapi ecosystem requiring Node.js v12 with the release of hapi 19

Version 3.2.0 - 2019-10-17

Added

  • basic TypeScript declarations in lib/index.d.ts

Version 3.1.5 - 2019-10-12

Updated

  • bump dependencies
  • minor refactorings
  • increase test timeouts
  • Readme: rename GitHub references fs-opensource -> futurestudio

Version 3.1.4 - 2019-04-24

Updated

  • update to hapi scoped dependencies
  • bump dependencies

Version 3.1.3 - 2019-02-18

Updated

  • bump dependencies
  • fix badges in Readme

Version 3.1.2 - 2019-01-26

Updated

  • Readme: rename GitHub references fs-opensource -> futurestudio

Version 3.1.1 - 2019-01-22

Added

  • test plugin for hapi 18

Updated

  • fix plugin subtitle in readme
  • remove NSP badge from readme
  • minor code changes
  • bump dependencies

Version 3.1.0 - 2018-12-06

  • add Node.js v11 as version in Travis CI
  • update refactor code to IP locator class
  • update dependencies: request-ip now supports more ways to find the client’s IP address
  • update tests: minor refactorings
  • remove NPM’s package-lock.json from repository

Version 3.0.2 - 2018-08-21

  • add keywords in package.json
  • update readme: reformat, quick navigation and logo size fix for small screens
  • update logo: minify file

Version 3.0.1 (2018-02-12)

  • update code to be use async/await instead of promise
  • update code examples in readme to async/await
  • bump dependencies

Version 3.0.0 (2017-11-06)

  • update readme to show hapi v17 functionality
  • update dependencies to newest versions
  • update Node.js versions on travis to 8 and 9

Version 3.0.0-rc.1 (2017-11-01)

  • update hapi-geo-locate to support hapi v17
  • update library and tests to support new async/await structure

Version 2.2.1 (2017-10-18)

  • add ESLint file .eslintrc.json for hapi-style code formatting
  • add option to lint code while testing (lab does that with the --lint flag)
  • add required Node.js to engines field in package.json (To make sure that NPM follows the Node.js v4+ requirement)
  • update code formatting to hapi-style
  • update dependencies

Version 2.2.0 (2017-05-30)

  • use request-ip package to determine the external IP address (request-ip supports a lot more ways to get the IP 👌)
  • update dependencies
  • update README and point out that all ways of determining the external IP address that are supported :)

Version 2.1.0 (2017-05-04)

  • add route option fakeIP to use a defined IP address for geo location

Version 2.0.0 (2017-05-01)

  • update route settings to listen on hapi-geo-locate instead of hapiGeoLocate (breaking change)
  • add logo

Migration for Breaking Changes

  • check your routes where your reference hapiGeoLocate and replace it with 'hapi-geo-locate'. With the new style, route options and plugin name match.

Version 1.1.2 (2017-03-24)

  • add project badges for TravisCI, Snyk and NSP (both checking for dependency vulerabilities), NPM
  • fix wrong import code in README
  • add 2017 to license

Version 1.1.1 (2017-02-12)

  • change extension point from onPreHandler to onPreAuth to determine the location before validation
  • improve tests for proxied requests and check explicitly for correct client IP address

Version 1.1.0 (2017-02-01)

  • detect client IP if request goes through multiple proxies (thank you Anton!)
  • add changelog to keep track of changes during releases

Version 1.0.0 (2016-12-27)

  • provide client location at request.location in route handler
  • option to have the plugin enabledByDefault
  • detect client IP from request.info.remoteAddress
  • detect client IP through x-forwared-for header