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

Wil*_*iam 29 node.js

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

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();
}
Run Code Online (Sandbox Code Playgroud)

soh*_*ifa 47

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

例如:

url.parse('http://stackoverflow.com/questions/17184791').pathname    
Run Code Online (Sandbox Code Playgroud)

会给你:

"/questions/17184791"
Run Code Online (Sandbox Code Playgroud)

  • @JonHoguet:你可以用反引号做到这一点:http://stackoverflow.com/editing-help#comment-formatting (3认同)
  • path和patha有什么区别 (2认同)
  • 那些网址实际上是http:// host/jon?v = 1,但我不知道如何让它开心 (2认同)

ben*_*ree 5

这是一个例子:

var url = "https://u:p@www.example.com: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
Run Code Online (Sandbox Code Playgroud)

还有一个:

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/
Run Code Online (Sandbox Code Playgroud)

Node.js文档:URL对象