In Node.js (or in any web server for that matter), is it somehow possible to be able to determine whether a resource request came from an embedded element in an html document, as opposed to a scripted request?
For instance, if I have this in my .html file:
<script src="/testRoute.js"></script>
And this in my javascript:
var xhr = new XMLHttpRequest();
xhr.open("GET", '/testRoute.js', true);
xhr.send();
is there a way the server can distinguish between these two requests?
The Same Origin Policy applies differently in each case, so there is apparently some differentiation going on (at least under the surface). Is there a way the server developer can see the difference?
Just as a personal experiment, using a diff tool, I compared an express req object received with each and saw only two slight differences:
The
socket,connection, andclientproperties in the<script>originatingreqobject had[function]s for theirerrorproperty, whereas these were[object]for the request that originated as anXMLHttpRequest.The
headersproperty containedcache-control: 'max-age=0'in the request that originated as anXMLHttpRequestbut not for the<script>request.
I don't really know what that means, and I don't know if these results are consistent across browsers, but they were consistent at least between Chrome and Firefox.