Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

request-ip

Package Overview
Dependencies
Maintainers
1
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

request-ip

A small Node.js module to retrieve the request's IP address

  • 3.3.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created

What is request-ip?

The request-ip npm package is used to retrieve a user's IP address from a request object in a Node.js application. It supports various types of requests and can extract the IP address from different sources such as headers, connection properties, and proxies.

What are request-ip's main functionalities?

Extract IP Address from Request

This feature allows you to extract the client's IP address from the request object in an Express.js application. The IP address is retrieved using the `getClientIp` method and can be used for logging, analytics, or other purposes.

const requestIp = require('request-ip');
const express = require('express');
const app = express();

app.use((req, res, next) => {
  const clientIp = requestIp.getClientIp(req);
  console.log(clientIp);
  next();
});

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

app.listen(3000, () => {
  console.log('Server is running on port 3000');
});

Support for Various Request Types

This feature demonstrates the ability to use the request-ip package with a plain Node.js HTTP server. The `getClientIp` method works seamlessly with different types of request objects, making it versatile for various Node.js applications.

const requestIp = require('request-ip');
const http = require('http');

const server = http.createServer((req, res) => {
  const clientIp = requestIp.getClientIp(req);
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end('Your IP address is: ' + clientIp);
});

server.listen(3000, () => {
  console.log('Server is running on port 3000');
});

Extract IP Address from Proxies

This feature shows how to configure an Express.js application to trust proxy headers and extract the client's IP address when the application is behind a proxy. The `trust proxy` setting is enabled, allowing the `getClientIp` method to correctly identify the client's IP address.

const requestIp = require('request-ip');
const express = require('express');
const app = express();

app.set('trust proxy', true);

app.use((req, res, next) => {
  const clientIp = requestIp.getClientIp(req);
  console.log(clientIp);
  next();
});

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

app.listen(3000, () => {
  console.log('Server is running on port 3000');
});

Other packages similar to request-ip

Keywords

FAQs

Package last updated on 07 Jul 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc