Url with parameters

Before heading out to what this post is going to be about, let me just give you one easy example of each type of URL.

Here is the path string param example:


Here is the same URL written in query string:


Now the best part of these two when combined with JavaScript is that you can get each part individually. What I mean by ‘part’ is that each URL is constructed with the following ‘parts’:


That same URL with our example is built as follow:

  • protocol: http/s
  • host: localhost:PORT or any URL, example:
  • pathname: auth/resetpassword
  • search: :userid/:resettoken

Now that you have an idea on how URLs are built, let’s break them apart.

Breaking an URL built on PATH:

const url = window.location.pathname.split('/');
const userid = url[0];
const token = url[1];
console.log({ userid, token});

By starting and splitting the pathname with slashes(/) we can get our values individually with no problem at all ONLY AFTER the main URL(auth/resetpassword).

Breaking an URL built on QUERY:

const url =;
const params = new URLSearchParams(url);
const userid = params.get('userid');
const token = params.get('resettoken');
console.log({userid, resettoken: token});

Using will fetch everything after the ? operator found on any URL.

That is it, feel free to share this article if you found it useful. Bye-Bye 🙂 .

If you want to know more about Web Development with NodeJS, take a look into this book:

Discover more from Kevin Uriel Fonseca

Subscribe now to keep reading and get access to the full archive.

Continue Reading

Back to Top