Getting URL Parameters in Node.js

You can get url parameters in Node by importing the built-in url module.

url Module — URL & URLSearchParams Objects

The url module gives access to the URL and URLSearchParams objects :

  • URL object contains properties to read different parts of a url — such as protocol, host, path, hash, search parameters (query parameters) etc
  • URLSearchParams object contains methods to perform operations on the search parameters in the url — getting parameters, adding new parameters, editing existing parameters or deleting parameters
const url = require('url');

// new URL object
const current_url = new URL('http://usefulangle.com/post/87/javascript-preview-pdf?id=123&type=article');

// reads the query string in the url
const query_string = current_url.search;

// will output : ?id=123&type=article
console.log(query_string);

// creating a new URLSearchParams object from query string
// this will read individual parameters
const search_params = new URLSearchParams(query_string); 

You can either create a new URLSearchParams object (like above) or use the searchParams property to get access to the URLSearchParams object :

const current_url = new URL('http://usefulangle.com/post/87/javascript-preview-pdf?id=123&type=article');

// same URLSearchParams object as above
const search_params = current_url.searchParams; 

Once you get access to the URLSearchParams object, you can get the url parameters.

Getting Parameters by Name

  1. You can get specific parameter values using the get method of the URLSearchParams object.

    const current_url = new URL('http://usefulangle.com/post/87/javascript-preview-pdf?id=123&type=article');
    const search_params = current_url.searchParams;
    
    // id = 123
    const id = search_params.get('id');
    
    // type = article
    const type = search_params.get('type');
    
  2. You can also check whether a parameter exists in the url using the has method. It returns a true or a false.

    const current_url = new URL('http://usefulangle.com/post/87/javascript-preview-pdf?id=123&type=article');
    const search_params = current_url.searchParams;
    
    // this will be true
    if(search_params.has('id')) {
        // this will be executed
    }
    
    // this will be false
    if(search_params.has('name')) {
        // this will not be executed
    }
    
  3. In case the parameter is multi-valued, you can use the getAll method. It returns an array of parameter values.

    const current_url = new URL('http://usefulangle.com/post/87/javascript-preview-pdf?tags=javascript&tags=pdfjs');
    const search_params = current_url.searchParams;
    
    // tags = Array [ "javascript", "pdfjs" ]
    const tags = search_params.getAll('tags');
    

Getting Parameters from the Request Url

You can use the URL object on the request url to get parameters in it.

const http = require('http');
const url = require('url');

const server = http.createServer((request, response) => {
    // this is the request url
    let request_url = request.url;

    // routing
    if(request.method === 'GET' && request_url === '/form') {
        const current_url = new URL(request_url);
        const search_params = current_url.searchParams;

        // "page_no" parameter
        if(search_params.has('page_no'))
            const page_no = search_params.get('page_no');

        // "num_results" parameter
        if(search_params.has('num_results'))
            const num_results = search_params.get('num_results');

        // rest of the code
    }
});

server.listen(8080);
Routing in Node.js (without framework)
Getting POST Parameters in Node.js