javascript expressjs:获取请求的 url

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12017111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 15:05:08  来源:igfitidea点击:

expressjs: get requested url

javascriptnode.jsexpress

提问by bodokaiser

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

我想从请求中获取客户端请求的 url。

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).

这根本不是很好。它还遗漏了我需要的 url 片段(#/something)。

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

有没有办法获得完整的网址?也许出标题?

Regards

问候

回答by Raoul

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

您无法在服务器上获取 url 的片段(哈希部分),浏览器不会传输它。

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.

片段标识符的功能与 URI 的其余部分不同:即,它的处理完全在客户端进行,没有服务器的参与——当然,服务器通常帮助确定 MIME 类型,而 MIME 类型确定片段的处理。当代理(例如 Web 浏览器)从 Web 服务器请求资源时,代理将 URI 发送到服务器,但不发送片段。而是代理等待服务器发送资源,然后代理根据文档类型和分片值处理资源。

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:

如果您想获得完整的 URL(没有片段标识符),最好的方法是使用包含端口的“Host”标头而不是 req.host ,但其他方面与您当前所做的相同:

var requestedUrl = req.protocol + '://' + req.get('Host') + req.url;

回答by lostintranslation

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.

现在您有了一个可以按需调用的函数。

回答by jamjam

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

恐怕这几乎是你所能做的。除非您使用 https,否则您可以假设协议为 http。正如 Raoul 所说,在 # 服务器端之后你什么也得不到,那是为了浏览器

   var requestedUrl =  'http://' + req.headers.host + ':3000'  + req.url

回答by 3on

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.

调试找出那种数据。