Node.js url.parse() 和 pathname 属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17184791/
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-09-02 14:49:55  来源:igfitidea点击:

Node.js url.parse() and pathname property

node.js

提问by William

I'm reading a getting started book on node.js called The Node Beginner Bookand in the code below (given in the book) I don't understand the significance of the pathname property hanging off the parse method. So I would like to know what it is doing. The documentationfor this method is not clear to me

我正在阅读一本关于 node.js的入门书,名为The Node Beginner Book,在下面的代码(书中给出)中,我不明白挂在 parse 方法上的 pathname 属性的重要性。所以我想知道它在做什么。我不清楚此方法的文档

var pathname = url.parse(request.url)**.pathname;** 

var http = require("http");
var url = require("url");
function start(route, handle) {
function onRequest(request, response) {
    var pathname = url.parse(request.url).pathname;         // I don't understand the pathname property
    console.log("Request for " + pathname + " received.");
    route(handle, pathname);
    response.writeHead(200, {"Content-Type": "text/plain"});
    response.write("Hello World");
    response.end();
}

回答by sohel khalifa

pathnameis the path section of the URL, that comes after the host and before the query, including the initial slash if present.

pathname是 URL 的路径部分,位于主机之后和查询之前,包括初始斜杠(如果存在)。

For example:

例如:

url.parse('http://stackoverflow.com/questions/17184791').pathname    

will give you:

会给你:

"/questions/17184791"

回答by bendytree

Here's an example:

下面是一个例子:

var url = "https://u:[email protected]:777/a/b?c=d&e=f#g";
var parsedUrl = require('url').parse(url);
...
protocol  https:
auth      u:p
host      www.example.com:777
port      777
hostname  www.example.com
hash      #g
search    ?c=d&e=f
query     c=d&e=f
pathname  /a/b
path      /a/b?c=d&e=f
href      https://www.example.com:777/a/b?c=d&e=f#g

And another:

还有一个:

var url = "http://example.com/";
var parsedUrl = require('url').parse(url);
...
protocol http:
auth     null
host     example.com
port     null
hostname example.com
hash     null
search   null
query    null
pathname /
path     /
href     http://example.com/

Node.js docs: URL Objects

Node.js 文档:URL 对象

回答by Ashita Gaur

pathnameis the part of URL section that comes after server and port. In,var pathname = url.parse(request.url).pathname; the request.url ,requests the url from the URL Section which is the set of the component - IP address of localhost , port no and file pathname.

路径名是位于服务器和端口之后的 URL 部分的一部分。在,var pathname = url.parse(request.url).pathname; request.url ,从 URL 部分请求 url,该部分是组件的集合 - localhost 的 IP 地址、端口号和文件路径名。

Let understand it by an example suppose this is the url to be requested to server http://127.0.0.1:8082/but for response to the client there should be an html file let it be index.html then http://127.0.0.1:8080/index.htmland this html file is the .pathname to the url. So,In var pathname = url.parse(http://127.0.0.1:8080/index.html).pathnamethe pathname is index.html that is response to client.

让我们通过一个例子来理解它假设这是要请求到服务器http://127.0.0.1:8082/的 url 但是为了响应客户端应该有一个 html 文件让它是 index.html 然后是 http://127.0 .0.1:8080/index.html这个 html 文件是 .pathname 到 url。所以,在 var pathname = url.parse( http://127.0.0.1:8080/index.html).pathname 中,路径名是 index.html ,它是对客户端的响应。

回答by Krishnamoorthy Acharya

if the following url is redirected in nodejs "http://localhost:9090/page/edit?pageId=1&type=edit"

如果以下 url 在 nodejs 中重定向“ http://localhost:9090/page/edit?pageId=1&type=edit

q.pathname will be "/page/edit" section of the URL. Please find other section of

q.pathname 将是 URL 的“/page/edit”部分。请找到其他部分

http.createServer(function (req, res) {
 var q = url.parse(req.url, true);
 console.log(q.pathname);
 // /page/edit
 console.log(q.query['type'])
 // edit
 console.log(q)
 //will show below attached image
})

enter image description here

在此处输入图片说明

回答by Harsh

url.parse(urlString[, parseQueryString[, slashesDenoteHost]])

urlString:The URL string to parse.

urlString:要解析的 URL 字符串。

parseQueryString :If true, the query property will always be set to an object returned by the querystring module's parse() method.

parseQueryString :如果为 true,则查询属性将始终设置为由 querystring 模块的 parse() 方法返回的对象。

slashesDenoteHost :If true, the first token after the literal string // and preceding the next / will be interpreted as the host

slashesDenoteHost :如果为 true,则文字字符串 // 之后和下一个 / 之前的第一个标记将被解释为主机

So, the url.parse() method takes a URL string, parses it, and returns a URL object.

因此, url.parse() 方法接受一个 URL 字符串,解析它,并返回一个 URL 对象。

Thus,

因此,

var pathname = url.parse(request.url).pathname;

will return the path name of the host followed by '/'

将返回主机的路径名后跟“/”

For example:

例如:

var pathname = url.parse(https://nodejs.org/docs/latest/api/url.html).pathname

will return:

将返回:

/docs//latest/api/url.html