- Node.js Tutorial
- Node.js - Home
- Node.js - Introduction
- Node.js - Environment Setup
- Node.js - First Application
- Node.js - REPL Terminal
- Node.js - Command Line Options
- Node.js - Package Manager (NPM)
- Node.js - Callbacks Concept
- Node.js - Upload Files
- Node.js - Send an Email
- Node.js - Events
- Node.js - Event Loop
- Node.js - Event Emitter
- Node.js - Debugger
- Node.js - Global Objects
- Node.js - Console
- Node.js - Process
- Node.js - Scaling Application
- Node.js - Packaging
- Node.js - Express Framework
- Node.js - RESTFul API
- Node.js - Buffers
- Node.js - Streams
- Node.js - File System
- Node.js MySQL
- Node.js - MySQL Get Started
- Node.js - MySQL Create Database
- Node.js - MySQL Create Table
- Node.js - MySQL Insert Into
- Node.js - MySQL Select From
- Node.js - MySQL Where
- Node.js - MySQL Order By
- Node.js - MySQL Delete
- Node.js - MySQL Update
- Node.js - MySQL Join
- Node.js MongoDB
- Node.js - MongoDB Get Started
- Node.js - MongoDB Create Database
- Node.js - MongoDB Create Collection
- Node.js - MongoDB Insert
- Node.js - MongoDB Find
- Node.js - MongoDB Query
- Node.js - MongoDB Sort
- Node.js - MongoDB Delete
- Node.js - MongoDB Update
- Node.js - MongoDB Limit
- Node.js - MongoDB Join
- Node.js Modules
- Node.js - Modules
- Node.js - Built-in Modules
- Node.js - Utility Modules
- Node.js - Web Module
- Node.js Useful Resources
- Node.js - Quick Guide
- Node.js - Useful Resources
- Node.js - Dicussion
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
To Continue Learning Please Login
Login with Google