
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Express.js – req.xhr Property
The req.xhr property is a Boolean property that returns True when the request's X-Requested-With header field is "XMLHttpRequest". The True pointer basically indicates that the request was issued by a client library such as jQuery.
Syntax
req.xhr
Example 1
Create a file with the name "reqXhr.js" and copy the following code snippet. After creating the file, use the command "node reqXhr.js" to run this code as shown in the example below −
// req.xhr Property Demo Example // Importing the express module var express = require('express'); // Initializing the express and port number var app = express(); // Initializing the router from express var router = express.Router(); var PORT = 3000; // Defining an endpoint app.get('/api', function (req, res) { console.log("Xhr is: ", req.xhr); res.send(req.xhr); }); app.listen(PORT, function(err){ if (err) console.log(err); console.log("Server listening on PORT", PORT); });
Hit the following Endpoint with a GET request − localhost:3000/api
Output
C:\home
ode>> node reqXhr.js Server listening on PORT 3000 Xhr is: false
Example 2
Let's take a look at one more example.
// req.xhr Property Demo Example // Importing the express module var express = require('express'); // Initializing the express and port number var app = express(); // Initializing the router from express var router = express.Router(); var PORT = 3000; // Defining an endpoint app.get('/api', function (req, res) { console.log("Xhr is: ", req.xhr); res.send(req.xhr); }); app.listen(PORT, function(err){ if (err) console.log(err); console.log("Server listening on PORT", PORT); });
Hit the following Endpoint with a GET request −
http://localhost:3000/api
and set the following headers −
X-Requested-With = XMLHttpRequest
Output
C:\home
ode>> node reqXhr.js Server listening on PORT 3000 Xhr is: true
Advertisements