npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

node-cloudflare-realip

v1.0.0

Published

Securely restore original visitor IPs when behind Cloudflare — middleware for Express, Fastify and raw http servers with IP range validation.

Readme

Node Cloudflare RealIP

Restore original visitor IP addresses when your Node.js app is behind Cloudflare. This package validates that the incoming connection comes from Cloudflare IP ranges before honoring Cloudflare headers like CF-Connecting-IP or True-Client-IP.

Why Use This Package?

When your application is behind Cloudflare, the direct connection comes from Cloudflare's servers, not the actual visitor. Cloudflare provides the real visitor IP in headers like CF-Connecting-IP. However, blindly trusting these headers is a security risk - anyone could spoof them.

This package:

  • Validates the connection is actually from Cloudflare by checking IP ranges
  • Only then trusts Cloudflare headers to get the real visitor IP
  • Works with Express, Fastify, and any Node.js HTTP server
  • Includes bundled Cloudflare IP ranges + ability to fetch latest ranges

Features

  • Secure: Validates Cloudflare IP ranges before trusting headers
  • Framework agnostic: Works with Express, Fastify, raw HTTP servers, and more
  • Lightweight: Minimal dependencies (only range_check for IP validation)
  • Auto-update: Fetch latest Cloudflare IP ranges on demand
  • TypeScript: Includes TypeScript definitions
  • Fast: Bundled ranges for offline operation

Installation

npm install node-cloudflare-realip

Quick Start

Express.js

const express = require('express');
const cloudflareRealIp = require('node-cloudflare-realip');

const app = express();

// Load Cloudflare IP ranges
cloudflareRealIp.load();

// Apply middleware
app.use(cloudflareRealIp.express());

app.get('/', (req, res) => {
  res.send('Your IP: ' + req.realIp);
});

app.listen(3000);

Fastify

const fastify = require('fastify')();
const cloudflareRealIp = require('node-cloudflare-realip');

cloudflareRealIp.load();
fastify.addHook('onRequest', cloudflareRealIp.fastify());

fastify.get('/', async (request, reply) => {
  return { ip: request.realIp };
});

fastify.listen({ port: 3000 });

Raw HTTP Server

const http = require('http');
const cloudflareRealIp = require('node-cloudflare-realip');

cloudflareRealIp.load();

http.createServer((req, res) => {
  if (cloudflareRealIp.check(req)) {
    req.realIp = cloudflareRealIp.get(req);
  }
  res.end('Your IP: ' + (req.realIp || req.socket.remoteAddress));
}).listen(3000);

API Reference

load([callback])

Load Cloudflare IP ranges from bundled ranges.json. Returns a Promise if no callback provided.

updateFromCloudflare()

Fetch the latest IP ranges from Cloudflare's official endpoints. Returns a Promise with updated ranges.

check(req)

Returns true if the request comes from a Cloudflare IP and has CF headers.

get(req)

Returns the real visitor IP from Cloudflare headers or falls back to socket address.

express()

Returns Express middleware that automatically sets req.realIp.

fastify()

Returns Fastify onRequest hook function.

Documentation

How It Works

  1. Checks if request has Cloudflare headers (CF-Connecting-IP or True-Client-IP)
  2. Validates the connection's IP address is in Cloudflare's IP ranges
  3. If validated, trusts the Cloudflare header and sets req.realIp
  4. Optionally overrides req.socket.remoteAddress for seamless integration

Security

This package only trusts Cloudflare headers when the connection originates from verified Cloudflare IP ranges. This prevents IP spoofing attacks where malicious users could send fake CF-Connecting-IP headers.

Publishing to npm

See docs/usage.md for publishing instructions.

License

MIT

Repository

https://github.com/ProgrammerNomad/node-cloudflare-realip

node-cloudflare-realip

Secure Node.js utility to restore real visitor IPs behind Cloudflare by validating Cloudflare IP ranges.