Node.js url.parse() and pathname property

后端 未结 5 1543
遇见更好的自我
遇见更好的自我 2020-12-14 06:33

I\'m reading a getting started book on node.js called The Node Beginner Book and in the code below (given in the book) I don\'t understand the significance

相关标签:
5条回答
  • 2020-12-14 06:52

    pathname is 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.

    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.html and this html file is the .pathname to the url. So,In var pathname = url.parse(http://127.0.0.1:8080/index.html).pathname the pathname is index.html that is response to client.

    0 讨论(0)
  • 2020-12-14 06:56

    if the following url is redirected in 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

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

    0 讨论(0)
  • 2020-12-14 06:59

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

    For example:

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

    will give you:

    "/questions/17184791"
    
    0 讨论(0)
  • 2020-12-14 07:01

    Here's an example:

    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
    

    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

    0 讨论(0)
  • 2020-12-14 07:08
    url.parse(urlString[, parseQueryString[, slashesDenoteHost]])
    

    urlString: The URL string to parse.

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

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

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

    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
    
    0 讨论(0)
提交回复
热议问题