Q37: What is `req.xhr` and how does Express detect AJAX requests?

Difficulty:EasyTopic:Request Processing

req.xhr is a boolean property that evaluates to true if the request's X-Requested-With header is set to XMLHttpRequest.

It allows route handlers to distinguish traditional server-rendered browser page navigations from asynchronous background API requests.

Key Takeaways
  • Checks the X-Requested-With: XMLHttpRequest header.
  • Useful when serving both HTML pages and JSON payloads from common route handlers.