Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between href and path in Url.parse() object?

Tags:

node.js

I have the following code written in NodeJS:

var Url = require("url");

// create server ... req, res

var queryData = Url.parse(req.url, true);
console.log(queryData);

// listen (...)

For the url from browser /test?param1=val1 the queryData is an object like this:

{ protocol: null,
  slashes: null,
  auth: null,
  host: null,
  port: null,
  hostname: null,
  hash: null,
  search: '?param1=val1',
  query: { param1: 'val1' },
  pathname: '/test',
  path: '/test?param1=val1',
  href: '/test?param1=val1' }

What's is the difference between path and href fields from parsed url object?

Is there any case when they are not the same?


EDIT: I see that in the documentation the href is "The full URL that was originally parsed. Both the protocol and host are lowercased.".

Example: 'http://user:[email protected]:8080/p/a/t/h?query=string#hash'

Why I don't get href like this? If I add a #hash to the url the href from the object is not changed.

like image 941
Ionică Bizău Avatar asked Oct 08 '13 05:10

Ionică Bizău


1 Answers

From the documentation:

href: The full URL that was originally parsed. Both the protocol and host are lowercased. Example: 'http://user:[email protected]:8080/p/a/t/h?query=string#hash'

path: Concatenation of pathname and search. Example: '/p/a/t/h?query=string'

like image 113
Dennis Avatar answered Sep 29 '22 12:09

Dennis