Q37: What is `req.xhr` and how does Express detect AJAX requests?
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: XMLHttpRequestheader. - Useful when serving both HTML pages and JSON payloads from common route handlers.