3

How do I get the full url , such as http://user:pass@host.com:8080/p/a/t/h?query=string#hash . Useing request.url can get /p/a/t/h?query=string ,but I hope to get full including #hash. What should I do?

var http = require('http');
var url = require('url');
http.createServer(function (request, response) {
   response.writeHead(200, {'Content-Type': 'text/plain'});
   response.end(request.url + '\n');
}).listen(8124);
Miser
  • 109
  • 7

2 Answers2

2

The hash is not sent to the server. You cannot get it server-side. (At least, not without some code running client-side sending it in another request over AJAX.)

Brad
  • 159,648
  • 54
  • 349
  • 530
  • ignoring the #hash - how does the op get the user:pass portion of the URL – Code Uniquely Nov 27 '14 at 10:19
  • @CodeUniquely That part is set in the `Authorization` header, base-64 encoded. – Brad Nov 27 '14 at 14:16
  • @CodeUniquely That's not how HTTP works. The full URL is never sent to the server. Only the "path" portion with query string is. If you don't believe me, fire up a packet sniffer like Wireshark and take a look. The `XHR.open()` method username/password are there for CORS requests. – Brad Nov 27 '14 at 16:28
1

The content after "#" is a bookmark on the client side and is not part of the URL sent to the server. You may want to read about "back button" problem to understand what it means. Something related to this on SO: Detecting Back Button/Hash Change in URL.

Community
  • 1
  • 1
Vikdor
  • 23,934
  • 10
  • 61
  • 84