18

I want to get the url the client has requested out of the request.

Currently I use:

var requestedUrl = req.protocol + '://' + req.host + ':3000' + req.url;

This isn't really nice at all. It also leaves out url fragments I require (#/something).

Is there a way I can get the complete url? Maybe out of the header?

Regards

bodokaiser
  • 15,122
  • 22
  • 97
  • 140
  • What are you trying to achieve? Usually I use a clientside routing framework like backbone or sammy which transparently uses either the history API or fragment identifiers depending on support. Then you can do content negotiation on your server to determine whether the request was made by ajax or not (and deliver appropriate content). – Raoul Aug 18 '12 at 11:42
  • I got the authentication routes on an subdomain and want to store the requested url into the session so I can redirect back from the login domain. in the frontend I use backbone too – bodokaiser Aug 18 '12 at 12:17

4 Answers4

25

You cannot get the fragment (hash section) of a url on the server, it does not get transmitted by the browser.

The fragment identifier functions differently than the rest of the URI: namely, its processing is exclusively client-side with no participation from the server — of course the server typically helps to determine the MIME type, and the MIME type determines the processing of fragments. When an agent (such as a Web browser) requests a resource from a Web server, the agent sends the URI to the server, but does not send the fragment. Instead, the agent waits for the server to send the resource, and then the agent processes the resource according to the document type and fragment value.

From Fragment Identifier on Wikipedia

If you want to get the complete URL (without the fragement identifier), the best way would be to use the "Host" header which includes the port rather than req.host but otherwise the same as you are currently doing:

var requestedUrl = req.protocol + '://' + req.get('Host') + req.url;
Raoul
  • 2,043
  • 1
  • 21
  • 29
  • 1
    Okay, so req.protocoll + '://' + req.host + req.url is the shortest way to achieve the whole url? – bodokaiser Aug 18 '12 at 12:16
  • yes, that will work, you can get the host from the headers too. – Raoul Aug 18 '12 at 13:00
  • In fact, from the docs: req.host returns the hostname from the "Host" header field (void of portno). So if you want to account for the port it's better to use the host header. – Raoul Aug 18 '12 at 13:05
  • so should I use req.get('Host') or req.host? – bodokaiser Aug 18 '12 at 16:03
  • 2
    If you are running the server on different ports in different environments then use req.get('Host') will contain the port number and you wont have to write logic to check the environment and adjust the port accordingly. – Raoul Aug 18 '12 at 16:40
3

I often times add something like this to my express app:

  app.use(function(req, res, next) {
    req.getRoot = function() {
      return req.protocol + "://" + req.get('host');
    }
    return next();
  });

Now you have a function you can call on demand if you need it.

lostintranslation
  • 23,756
  • 50
  • 159
  • 262
  • I wonder why you'd need a function on demand, why not store it in a variable and use the variable on demand. – joedotnot Feb 15 '17 at 19:46
1

I'm afraid that pretty much all you can do. Unless you using https you can assume the protocol as http. As Raoul said you are not able to get anything after # server-side, that's for the browser

   var requestedUrl =  'http://' + req.headers.host + ':3000'  + req.url
jamjam
  • 3,171
  • 7
  • 34
  • 39
  • Using the "Host" header will ensure any port number is preserved and you won't need to maintain that in 2 places. Also he mentioned above that this is for login, so hopefully that means it's an https connection. Either way, req.protocol provides the URL scheme. – Raoul Aug 18 '12 at 13:08
0

Is this that you are looking for ?

req._parsedUrl.pathname

You should, if you don't already use:

var util = require('util'); // core module
console.log(util.inspect(req));

To debug find out that kind of data.

3on
  • 6,291
  • 3
  • 26
  • 22
  • hmm the first one only gives the url itself. I used youre hint with util to parse manuelly "req". I didn't found what I was looking for. I want the whole ressource (protocol + hostname + url + hashbang) – bodokaiser Aug 18 '12 at 08:40