How to encode or decode a URL in Node.js

URL encoding is commonly used to avoid cross-site scripting (XSS) attacks by encoding special characters in a URL. It converts a string into a valid URL format making the transmitted data more reliable and secure.

In this article, you'll learn how to encode or decode a URL string and query string parameters in a Node.js application.

URL Encoding

Since Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine, you can use JavaScript methods such as encodeURI() and encodeURIComponent() to encode a URL.

encodeURI Method

The encodeURI() method encodes a complete URL, including encodes special characters except ~!$&@#*()=:/,;?+ characters:

const url = '!Learn Node$/ Example'

// Encode complete URL
const encodedUrl = encodeURI(url)

// Print encoded URL


encodeURIComponent() Method

You should use the encodeURIComponent() method to encode special characters in URI components. This method is suitable for encoding URL components such as query string parameters and not the complete URL.

Here is an example:

const baseUrl = ''
const query = 'SELECT * from users WHERE id = 1'

// Encode query string
const encodedQuery = encodeURIComponent(query)

// Build full URL
const url = baseUrl + encodedQuery

// Print full URL


querystring Module

You could also use the Node.js built-in querystring module to encode a URL. This module provides utility methods for parsing and formatting URL query strings:

const querystring = require('querystring')

const baseUrl = ''
const query = 'SELECT * from users WHERE id = 1'

// Encode query string
const encodedQuery = querystring.stringify({ q: query })

// Build full URL
const url = baseUrl + encodedQuery

// Print full URL


URL Decoding

URL decoding is the opposite of the encoding process. It converts the encoded URL strings and query parameters back to their regular formats.

JavaScript provides decodeURI() and decodeURIComponent() that can also be used in Node.js to perform URL decoding.

decodeURI Method

The decodeURI() method is used to decode a complete URL:

const encodedUrl = '!Learn%20Node$/%20Example'

// Decode URL
const url = decodeURI(encodedUrl)

// Print URL

//!Learn Node$/ Example

decodeURIComponent() Method

The decodeURIComponent() function is used to decode URL components:

const query = 'Dankeschön für Ihre €100'

// perofrm encode/decode
const encodedStr = encodeURIComponent(query)
const decodedStr = decodeURIComponent(encodedStr)

// print values
console.log(`Encoded Query: ${encodedStr}`)
console.log(`Decoded Query: ${decodedStr}`)

// Output
// Encoded Query: Dankesch%C3%B6n%20f%C3%BCr%20Ihre%20%E2%82%AC100
// Decoded Query: Dankeschön für Ihre €100

✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.

You might also like...

Digital Ocean

The simplest cloud platform for developers & teams. Start with a $200 free credit.

Buy me a coffee ☕

If you enjoy reading my articles and want to help me out paying bills, please consider buying me a coffee ($5) or two ($10). I will be highly grateful to you ✌️

Enter the number of coffees below:

✨ Learn to build modern web applications using JavaScript and Spring Boot

I started this blog as a place to share everything I have learned in the last decade. I write about modern JavaScript, Node.js, Spring Boot, core Java, RESTful APIs, and all things web development.

The newsletter is sent every week and includes early access to clear, concise, and easy-to-follow tutorials, and other stuff I think you'd enjoy! No spam ever, unsubscribe at any time.