Express.js req.get() Function
Last Updated :
08 Jan, 2025
The req.get() function returns the specified HTTP request header field which is a case-insensitive match and the Referrer and Referrer fields are interchangeable.
Syntax:
req.get( field )
Parameter: The field parameter specifies the HTTP request header field.
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.get('Content-Type'));
res.end();
});
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 make a GET request to http://localhost:3000/ with the header set to 'content-type: text/plain', then you will see the following output on your console:
Server listening on PORT 3000
text/plain
Example 2: Filename: index.js
javascript
const express = require('express');
const app = express();
const PORT = 3000;
app.get('/', function (req, res) {
console.log(req.get('Anything-else'));
res.end();
});
app.listen(PORT, function (err) {
if (err) console.log(err);
console.log("Server listening on PORT", PORT);
});
Steps to run the program:
Run the index.js file using the below command:
node index.js
Now make a GET request to http://localhost:3000/, then you will see the following output on your console:
Output:
Server listening on PORT 3000
undefined
Reference: https://expressjs.com/en/4x/api.html#req.get
Similar Reads
Express app.get() Request Function The app.get() function is used to define routes on your server that handle HTTP GET requests. A GET request is typically used when the client asks the server to send back some information, like retrieving a webpage or data from a database.Itâs an essential part of building websites and APIs, as it a
6 min read
Express app.use() Function The app.use() function in Express.js adds middleware to the application's request-processing pipeline. It applies the specified middleware to all incoming requests or to specific routes, allowing you to modify request/response objects, perform operations, or handle errors throughout the application.
3 min read
Express res.end() Function The res.end() function concludes the response process and is derived from the HTTP.ServerResponse's response.end() method in the Node core. It is employed to promptly conclude the response without including any data.Syntax: res.end([data] [, encoding])Parameters: The default encoding is 'utf8' and t
2 min read
Express res.status() Function The res.status() function sets the HTTP status for the response. It is a chainable alias of Nodeâs response.statusCode. Syntax:res.status( code )Parameter: This function accepts a single parameter code that holds the HTTP status code. Return Value: It returns an Object. Steps to create the Express A
2 min read
Express.js express.text() Function The express.text() function is a built-in middleware in Express.js that parses incoming HTTP request bodies with a text/plain content type. It allows you to easily handle raw text data sent in the body of a request, making it suitable for handling non-JSON, non-URL-encoded, or non-multipart data.The
5 min read
Express app.listen() Function In Express.js, the app.listen() function is used to start a server and make it listen for incoming requests on a specified port and host. This method is essential for running an Express application as it enables the server to handle HTTP requests from clients (like browsers).Syntax: app.listen([port
3 min read