I was recently surprised to find that req.url
(inheritied from node's http module) and req.originalUrl
(a copy of req.url
that is preserved from rewriting by middleware, for example) can return an absolute URL rather than a path.
This is because message.url
in node's http module is simply the URL of the GET
request, and apparently (if I'm readying right) a GET
request can have an absolute URL and still be valid. (Edit: yes, MDN confirms my understanding here.)
So is it true that req.url and req.originalUrl are simply exact copies of the GET
URL? If so, I'm really surprised that every answer on this question, including ones with hundreds of upvotes say that you should get the full URL like so:
req.protocol + '://' + req.get('host') + req.originalUrl
Given that nearly 500k people have viewed that question without making a comment or answer, it seems like I must be missing something here. But then, reading this GitHub issue, it seems that my interpretation is correct.
So I'm basically asking this question to confirm my understanding so I can write an answer/comment/edit on that linked question and hopefully help others avoid the problems that I had that result from using code like the above without handling the possibility of an absolute URL.