A hapi plugin to geo-locate requests
Follow @marcuspoehls for updates!
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.
hapi v19 (or later) and Node.js v12 (or newer)
This plugin requires hapi v19 (or later) and Node.js v12 or newer.
|Major Release||hapi.js version||Node.js version|
hapi-geo-locate as a dependency to your project:
npm i hapi-geo-locate
Using hapi v17 or v18?
3.x release line:
npm i hapi-geo-locate@3
Do you use hapi v16 (or lower)?
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
The most straight forward way to register the
await server// went smooth like dark chocolate :)// Within your route handler functions, you can access the location like thisserver
Plugin Registration Options
The following plugin options allow you to customize the default behavior of
true— by default, the plugin geo locates the request by IP on every request
(string), no default — the API token to authenticate requests against the IPinfo API
await server// Within your route handler functions, you can access the location like thisserver
Route Handler Options
The following plugin options on individual route handlers allow you to customize the behavior of
(boolean)— tells the plugin to enable (
true) or disable (
false) geo location for the request by IP
(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
server// Within your route handler functions, you can access the location like thisserver
Supported Proxies and Proxy Headers
hapi-geo-locate supports all proxies that request-ip does:
X-Forwarded-For, picking the first, client IP if the request went through multiple proxies.
Forwardedas variations of
- and all the
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 :)
Do you miss a feature? Please don’t hesitate to create an issue with a short description of your desired addition to this plugin.
Links & Resources
- hapi tutorial series (100+ tutorials)
- node-ipinfo: Node.js wrapper for the ipinfo.io API
- request-ip: Node.js module for retrieving a request’s IP address
- Create a fork
- Create your feature branch:
git checkout -b my-feature
- Commit your changes:
git commit -am 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- Submit a pull request 🚀
MIT © Future Studio