How to distinguish HTTP Request and XML HTTP Request in Node.js using express?

In my code for some reason I want to distinguish normal HTTP Request and AJAX Request (i.e XML HTTP Request).

After searching a lot, I have tried to use req.isXMLHttpRequest, but for both type of Request it returns undefined.

Now I am handling it by adding a flag in each and every HTTP Request as isAjax with true or false value.

Question: What is the best suggested way?

  1. To use adding flag (isAJAX) to each XML HTTP request (with true or false)

  2. Find out and fix req.isXMLHttpRequest related issues.

Will be of great help if anyone tell me, Why req.isXMLHttpRequest is returning undefined?

Advanced Thanks

Note: I am using Node.JS Version 0.8.20 and Express Version 3.1.0

You could try this.

req.xhr

this will return true or false if it is an XMLHttpRequest or not.

Following @scartag answer. If you control whoever sending the request (e.g., it's your client code) then you can just set the HTTP header X-Requested-With: xmlhttprequestor change the body/url to mark the xhr request.

If you don't control whoever is sending the request you can't rely on req.xhr then your options depend on your use case. For instance, if you just want to respond with a content that is appropriate to the request then res.format(_) could be more useful than req.xhr (it depends on your clients, but you can probably assume that clients won't request a format that they cannot handle).