Parsing request paths and query parameters with URL in Node.js

Parse relative request URLs in Node.js and retrieve their paths and query parameters.

🎯 Introduction

By the end of this article, you will be able to parse relative request URLs in Node.js and retrieve their paths and query parameters.


🧱 The Node.js difference: no current page URL

Node.js uses the same standard URL and URLSearchParams APIs as browsers. The issue here is that an incoming request may contain only /users?page=2, and Node.js has no current browser page URL to complete it.

Provide an explicit base URL when parsing these request URLs.

Use a request URL string to simulate input below. Save it as request-url.mjs, append the sections in order, and run node request-url.mjs.


🛠 Provide a base for relative request URLs

const requestUrl = '/users?page=2&tag=js&tag=node'
const url = new URL(requestUrl, 'http://localhost:3000')
console.log(url.pathname)

This prints /users. The first argument is the input; the second supplies the protocol and host. Constructing a URL object only parses the address; it does not send a request to localhost.

Using only new URL(requestUrl) throws an invalid URL error because the relative URL lacks a base. When handling service requests, use a base explicitly configured by the application. Do not blindly trust client-supplied host information when generating public links.


🔍 Read parameters for request handling

Retrieve the page number:

console.log(url.searchParams.get('page'))

This prints 2, but the return value is the string '2'. Missing parameters return null. Convert and validate the page range before querying; successful parsing does not mean valid input.

Handle repeated parameters according to the API contract. This example allows multiple tags, so use getAll to retrieve them:

console.log(url.searchParams.getAll('tag'))

This prints ['js', 'node']; get('tag') returns only the first value, 'js'. Specify whether each API parameter accepts one or multiple values to avoid losing filters.

When constructing outgoing requests, use the familiar URLSearchParams to set parameters. Node.js requires no separate concatenation technique, and you should not concatenate unencoded user input manually.


❓ Questions and answers

Q1: Are values after the question mark converted to numbers automatically?

No. Convert and validate them according to their meaning in your application.

Q2: Why can I not pass a relative request URL alone to new URL?

It lacks a protocol and host. Node.js has no current page URL as a default base, so supply one explicitly.

Q3: Does successful URL parsing mean the address is safe to request?

No. Separately check allowed protocols, hosts, and other access conditions.


🧪 Exercise

Add a tag to the simulated request. Then remove page and change page to abc in separate tests. Read the results and explain in comments whether the backend should accept or reject each input.

// Write your code here

🎉 Try these techniques with your own examples!