How to parse URL parameters in Node.js

Parsing URL parameters correctly is essential for building robust APIs and handling user input in Node.js applications. As the creator of CoreUI, a widely used open-source UI library, I’ve built countless backend services and API endpoints over 25 years of development. From my expertise, the most reliable approach is using Node.js built-in URL constructor with URLSearchParams to safely parse query strings and route parameters. This ensures proper handling of encoded characters and special cases.

Use Node.js built-in URL constructor with URLSearchParams to parse URL parameters.

const url = new URL(req.url, `http://${req.headers.host}`)
const params = url.searchParams
const userId = params.get('userId')

Here the URL constructor creates a proper URL object from the request URL and host header. The searchParams property gives you a URLSearchParams object with methods like get() to retrieve specific parameter values. This approach automatically handles URL decoding and provides a clean API for working with query parameters.

Best Practice Note:

This is the same URL parsing approach we use in CoreUI backend integrations for reliable parameter extraction. Always validate and sanitize URL parameters before using them in your application logic, and consider using libraries like Joi or Yup for comprehensive input validation.


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author