- 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.pathname property
The NodeJS url.pathname property of URL class allows us to get and set the pathname segment of the given URL.
Invalid URL characters included in the pathname segment assigned to this property are percent-encoded. If an invalid URL is specified, this property will throw a TypeError.
Syntax
Following is the syntax of the NodeJS pathname property of URL class
URL.pathname
Parameters
This property does not accept any parameters.
Return Value
This property can get and set the pathname portion of the provided URL.
Example
If we assign a complete URL to the NodeJS url.pathname property, it will get the pathname portion of the given URL.
In the following example, we are trying to assign a URL to the NodeJS url.pathname property.
const url = require('url'); const myURL = new URL("https://www.tutorialspoint.com/prime-pack/cloud-computing/index.asp"); console.log("Path segment of the URL: " + myURL.pathname);
Output
After executing the above program, the NodeJS pathname property gets the pathname segment from the provided URL.
/prime-pack/cloud-computing/index.asp
Example
If we try to set the path to an URL, the pathname property will return the complete URL with provided path.
In this program below, we are modifying the path of the URL by using the pathname property.
const url = require('url'); const myURL = new URL("https://www.tutorialspoint.com/prime-pack/cloud-computing"); console.log(myURL.href) console.log("Path segment of the URL: " + myURL.pathname); myURL.pathname = "/Normal-pack/Node-js"; console.log("After setting the pathname to : " + myURL.pathname); console.log("The modified path will be: " + myURL.href);
Output
As we can in the output below, the path of the input URL got modified.
https://www.tutorialspoint.com/prime-pack/cloud-computing Path segment of the URL: /prime-pack/cloud-computing After setting the pathname to : /Normal-pack/Node-js The modified path will be: https://www.tutorialspoint.com/Normal-pack/Node-js
Example
If any invalid URL characters are provided in the pathname portion, the pathname property will get the URL by percent-encoding the invalid characters.
In the following example, we are assigning an URL with invalid characters in the pathname portion.
const url = require('url'); const myURL = new URL("http://www.tutorialspoint.com/Node`Js"); console.log(myURL.href) console.log("Path segment of the URL: " + myURL.pathname);
Output
As we can see in the output, the invalid characters in the URL got percent-encoded.
http://www.tutorialspoint.com/Node%60Js Path segment of the URL: /Node%60Js
Example
If we assign an invalid URL to the pathname property, it returns a TypeError.
In the example below, we are assigning an invalid URL to the pathname property.
const url = require('url'); const myURL = new URL("[ww.tutorialspoint.com/Node`Js"); console.log(myURL.href) console.log("Path segment of the URL: " + myURL.pathname);
TypeError
As we can see in the output below, a TypeError is thrown by the pathname 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: '[ww.tutorialspoint.com/Node`Js', code: 'ERR_INVALID_URL' }
To Continue Learning Please Login
Login with Google