Giter Club home page Giter Club logo

forwarded-for's Introduction

forwarded-for

Version npmBuild StatusDependenciesCoverage StatusIRC channel

When you are hosting your applications behind a reverse load balancer the incoming requests will no longer have the IP address of your user but of the load balancer as it forwards the request to your node instance. Most load balancers allow you to modify the headers of the request and add original request information in it.

This module makes it easier to find the correct IP address of your connection by detecting these headers that load balancers set and it will gracefully fall down to the original req.address that contained the IP address of the request inside node.js. In addition to gracefully degrading to the original request we also for other known locations of the address (which are usually set by frameworks such as SockJS and Socket.IO etc.)

Installation

The module is released in the npm registry:

npm install --save forwarded-for

Getting started

Let's start out with including the module in to your application:

'use strict';

var forwarded = require('forwarded-for');

The forwarded variable will now contain the function which parses out the request information for you in to a really simple object. This object contains the following properties:

  • port The remote port. It defaults to 0.
  • ip The string representation of the remoteAddress. It defaults to '127.0.0.1'.

When we fail to parse or detect an IP address we will use the default values to construct the object. To correctly parse the data the function requires the following arguments:

  • obj The socket like object that probably contains the remoteAddress
  • headers A reference to the HTTP headers of the request
  • whitelist, optional A white list of IP addresses of your load balancers so people cannot make fake requests with x-forwarded-for headers.

So with all the information combined, it would look something like this:

'use strict';

var forwarded = require('forwarded-for');

require('http').createServer(function (req, res) {
  var address = forwarded(req, req.headers);

  res.end('Your ip address is '+ address.ip);
}).listen(8080);

License

MIT

forwarded-for's People

Contributors

3rd-eden avatar greenkeeper[bot] avatar lpinca avatar mrlannigan avatar ronkorving avatar stevenvergenz 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.