• Node.js Video Tutorials

NodeJS - url.origin Property



The NodeJS url.origin property of URL class is used to get the read-only serialization of the URL’s origin.

This property doesn’t allow to set the read-only serialization of the URL’s origin. If we try to set a new origin to the URL, it will get ignored and the existing origin will not be affected. If any value is assigned other than a valid URL to the origin property, a TypeError will be thrown.

Syntax

Following is the syntax of the NodeJS origin property of URL class

URL.origin

Parameters

This property does not accept any parameters.

Return Value

This property only gets the read-only serialization of the URL’s origin.

The Below examples demonstrate the usage of the NodeJS URL.origin property of the path module.

Example

If we assign a URL to the NodeJS origin property, it will return only the read-only serialization of the given URL’s origin.

In the following example, we are trying to print the origin of the input URL.

const url = require('url');

const myURL = new URL("https://www.tutorialspoint.com/index.htm");
console.log(myURL.origin);

Output

As we can see in the output below, the origin property returns the given URL’s origin.

https://www.tutorialspoint.com

Example

If we try to set the origin of the URL using the URL.origin property, it won’t allow to do it and then it will be ignored. The origin will not be affected.

In the following example, we are trying to modify the origin of the given URL by using the URL.origin property.

const url = require('url');

const myURL = new URL("https://www.tutorialspoint.com/index.htm");
console.log("Before setting the Origin: " + myURL.origin);

myURL.origin = "https://www.tutorix.com/index.htm";
console.log("After setting a new Origin to myURL: " + myURL.origin);

Output

As we can see in the output below, the origin property doesn’t allow to set a new origin to the input URL.

Before setting the Origin: https://www.tutorialspoint.com
After setting a new Origin to myURL: https://www.tutorialspoint.com

Example

If we assign a value that is not a valid URL type to the origin property, then the origin property will throw a TypeError.

In the given program, we are assigning an invalid URL to the origin property.

const url = require('url');

const myURL = new URL(2746832);
console.log("The origin: " + myURL.origin);

TypeError

As we can see in the output below, an TypeError is thrown by the origin property.

node:internal/url:564
  throw new ERR_INVALID_URL(input);
  ^

TypeError [ERR_INVALID_URL]: Invalid URL
    at new NodeError (node:internal/errors:387:5)
    at URL.onParseError (node:internal/url:564:9)
    at new URL (node:internal/url:640:5)
    at Object.<anonymous> (C:\Users\Lenovo\Desktop\JavaScript\nodefile.js:3:15)
    at Module._compile (node:internal/modules/cjs/loader:1126:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1180:10)
    at Module.load (node:internal/modules/cjs/loader:1004:32)
    at Function.Module._load (node:internal/modules/cjs/loader:839:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:17:47 {
  input: '2746832',
  code: 'ERR_INVALID_URL'
}

Example

If Unicode characters appear in the hostname of the input URL, then it will be automatically converted to ASCII.

In the following program, we are trying to assign a URL containing Unicode characters to the origin property.

const url = require('url');

const myURL = new URL("https://おねがいします");
console.log(myURL.origin);

Output

If you execute the above program, the output is displayed as follows.

https://xn--n8jlg9bk0j8e
nodejs_url_module.htm
Advertisements