Handling duplicate query string arguments in Express.js

Duplicate query string entries are somewhat common, for example if you use Solr, you can do something like this:


If you make an Express.js endpoint, you get can this data through the “query” option:

  function(req, res) {

The “req.query” is a Javascript object, which presents difficulties with duplicated arguments, which are joined into an array (so you do lose some information about order).

If, for example, you wanted to combine all back into a string, you could do it with something like this (assuming you use underscore/lodash):

let args = req.query;
let queryString =
       (arg) => 
         isArray(args[arg]) ? 
             (argv) => arg + '=' + argv 
         '&') : 
         arg + '=' + args[arg]), 

If you want a good Javascript book, I got a lot out of Secrets of the Javascript Ninja.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *