Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add header pioritizing, update README, example and added test case. #76

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 16 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# request-ip

A tiny Node.js module for retrieving a request's IP address.
A tiny Node.js module for retrieving a request's IP address.

![](https://nodei.co/npm/request-ip.png?downloads=true&cacheBust=3)

Expand All @@ -12,53 +12,56 @@ A tiny Node.js module for retrieving a request's IP address.
## Installation

Yarn

Chheung marked this conversation as resolved.
Show resolved Hide resolved
```
yarn add request-ip
```

npm

```bash
npm install request-ip --save
```

## Getting Started

```javascript
const requestIp = require('request-ip');

// inside middleware handler
const ipMiddleware = function(req, res, next) {
const clientIp = requestIp.getClientIp(req);
next();
const ipMiddleware = function (req, res, next) {
const clientIp = requestIp.getClientIp(req);
next();
};

// on localhost you'll see 127.0.0.1 if you're using IPv4
// on localhost you'll see 127.0.0.1 if you're using IPv4
// or ::1, ::ffff:127.0.0.1 if you're using IPv6
```

### As Connect Middleware

```javascript
const requestIp = require('request-ip');
app.use(requestIp.mw())
app.use(requestIp.mw());

app.use(function(req, res) {
const ip = req.clientIp;
res.end(ip);
app.use(function (req, res) {
const ip = req.clientIp;
res.end(ip);
});
```

To see a full working code for the middleware, check out the [examples](https://github.com/pbojinov/request-ip/tree/master/examples) folder.

The connect-middleware also supports retrieving the ip address under a custom attribute name, which also works as a container for any future settings.
The connect-middleware also supports retrieving the ip address under a custom attribute name, which also works as a container for any future settings.
And it also support header check priority.

## How It Works

It looks for specific headers in the request and falls back to some defaults if they do not exist.

The user ip is determined by the following order:

1. `X-Client-IP`
1. `X-Client-IP`
2. `X-Forwarded-For` (Header may return multiple IP addresses in the format: "client IP, proxy 1 IP, proxy 2 IP", so we take the first one.)
3. `CF-Connecting-IP` (Cloudflare)
4. `Fastly-Client-Ip` (Fastly CDN and Firebase hosting header when forwared to a cloud function)
Expand All @@ -78,8 +81,7 @@ If an IP address cannot be found, it will return `null`.

## Samples Use Cases

* Getting a user's IP for geolocation.

- Getting a user's IP for geolocation.

## Running the Tests

Expand Down
20 changes: 15 additions & 5 deletions examples/connect-middleware-custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,32 @@ var requestIp = require('request-ip');

// you can override which attirbute the ip will be set on by
// passing in an options object with an attributeName
app.use(requestIp.mw({ attributeName : 'myCustomAttributeName' }))
app.use(
requestIp.mw({
attributeName: 'myCustomAttributeName',
prioritize: ['x-real-ip'],
}),
);

// respond to all requests
app.use(function(req, res) {

app.use(function (req, res) {
// use our custom attributeName that we registered in the middleware
var ip = req.myCustomAttributeName;
console.log(ip);

// https://nodejs.org/api/net.html#net_net_isip_input
var ipType = net.isIP(ip); // returns 0 for invalid, 4 for IPv4, and 6 for IPv6
res.end('Hello, your ip address is ' + ip + ' and is of type IPv' + ipType + '\n');
res.end(
'Hello, your ip address is ' +
ip +
' and is of type IPv' +
ipType +
'\n',
);
});

//create node.js http server and listen on port
http.createServer(app).listen(3000);

// test it locally from the command line
// > curl -X GET localhost:3000 # Hello, your ip address is ::1 and is of type IPv6
// > curl -X GET localhost:3000 # Hello, your ip address is ::1 and is of type IPv6