expressjs: get requested url

后端 未结 4 1902
情深已故
情深已故 2021-02-19 07:02

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

Currently I use:

var requestedUrl = req.protocol + \'://\' + req.host + \':3000\' + re         


        
4条回答
  •  臣服心动
    2021-02-19 07:41

    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;
    

提交回复
热议问题