The Wayback Machine - https://web.archive.org/web/20240711142857/https://www.geeksforgeeks.org/express-js-req-ip-property/
Open In App

Express.js req.ip Property

Last Updated : 17 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The req.ip property contains the remote IP address of the request. It is useful when the user wants the IP address of the incoming request made to the application. 

Syntax:

req.ip

Parameter: No parameter. 

Return Value: String 

Installation of the express module:

You can visit the link to Install the express module. You can install this package by using this command.

npm install express

After installing the express module, you can check your express version in the command prompt using the command.

npm version express

After that, you can just create a folder and add a file, for example, index.js. To run this file you need to run the following command.

node index.js

Project Structure:

Example 1: Filename: index.js 

javascript




const express = require('express');
const app = express();
const PORT = 3000;
 
app.get('/', function (req, res) {
    console.log(req.ip);
    res.send();
});
 
app.listen(PORT, function (err) {
    if (err) console.log(err);
    console.log("Server listening on PORT", PORT);
});


Steps to run the program:

Make sure you have installed the express module using the following command:

npm install express

Run the index.js file using the below command:

node index.js

Output:

Console Output:

Server listening on PORT 3000

Browser Output:

Now open your browser and go to http://localhost:3000/, now you can see the following output on your console:

Server listening on PORT 3000
::1

Example 2: Filename: app.js 

javascript




const express = require('express');
const app = express();
const PORT = 3000;
 
app.enable('trust proxy')
app.get('/', function (req, res) {
    console.log(req.ip);
    res.send();
});
 
app.listen(PORT, function (err) {
    if (err) console.log(err);
    console.log("Server listening on PORT", PORT);
});


Steps to run the program:

Run the app.js file using the below command:

node app.js

Output:

Console Output:

Server listening on PORT 3000

Now open your browser and make a GET request to http://localhost:3000/ with the header set to x-forwarded-for: 203.0.113.195, now you can see the following output on your console:

Browser Output:

Server listening on PORT 3000
203.0.113.195

Reference: https://expressjs.com/en/4x/api.html#req.ip



Similar Reads

Difference between req.cookies and req.signedCookies in Express.js
req.cookies: Request. Cookies are supposed to be cookies that come from the client (browser) and Response. Cookies are cookies that will send back to the client (browser). Cookies are small files/data that are sent to the client with a server request and stored on the client side. This helps us to keep track of the user’s actions. Cookie-parser is
3 min read
Difference between req.query and req.params in Express
In this article, we will learn about the req.query and req.params and we will also learn the difference between them. Table of Content req.query: Dealing with URL Stuffreq.params: Figuring Out Route ThingsDifference between req.query and req.params in Express.jsreq.query: Dealing with URL Stuff:Where the Data Comes From: It's from the extra bits at
3 min read
Express.js req.subdomains Property
The req.subdomains property contains an array of subdomains in the domain name of the request. The application property subdomain offset, which defaults to 2 determines the beginning of the subdomain segments. Syntax: req.subdomains Parameter: No parameters. Returns: Array Installation of the express module: You can visit the link to Install the ex
2 min read
Express.js req.stale Property
The req.stale property indicates whether the incoming request is stale, and this property is the opposite of req.fresh property. Syntax: req.stale Parameter: No parameters. Return Value: True or False. Installation of the express module: You can visit the link to Install the express module. You can install this package by using this command. npm in
2 min read
Express.js req.secure Property
The req.secure property is a Boolean property that is true if a TLS connection is established else returns false. Syntax: req.secure Return Value: It returns a Boolean value either True or False. Installation of the express module: You can visit the link to Install the express module. You can install this package by using this command. npm install
2 min read
Express.js req.baseUrl Property
The req.baseUrl property is the URL path on which a router instance was mounted. The req.baseUrl property is similar to the mount path property of the app object, except app.mountpath returns the matched path pattern(s). Syntax: req.baseUrl Parameter: No parameters. Return Value: String Installation of the express module: You can visit the link to
2 min read
Express.js req.protocol Property
The req.protocol property contains the request protocol string which is either HTTP or (for TLS requests) https. When the trust proxy setting does not evaluate to false, this property will use the X-Forwarded-Proto header field value if it is present. Syntax: req.protocol Parameter: No parameters. Returns: String. Installation of the express module
2 min read
Express.js req.xhr Property
The req.xhr property returns a true value if the request’s X-Requested-With header field is XMLHttpRequest which indicates that the request was issued by a client library such as jQuery. Syntax: req.xhr Parameter: No parameters. Returns: True or False. Installation of the express module: You can visit the link to Install the express module. You can
2 min read
Express.js req.cookies Property
The req.cookies property is used when the user is using cookie-parser middleware. This property is an object that contains cookies sent by the request. Syntax: req.cookies Parameter: No parameters. Return Value: Object Installation of the express module: You can visit the link to Install the express module. You can install this package by using thi
2 min read
Express.js req.app Property
The req.app property holds the reference to the instance of the Express application that is using the middleware. Syntax: req.app Parameter: No parameters. Return Value: Object Installation of the express module: You can visit the link to Install the express module. You can install this package by using this command. npm install express After insta
2 min read
three90RightbarBannerImg