{"id":3513,"date":"2016-03-27T23:37:02","date_gmt":"2016-03-27T23:37:02","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=3513"},"modified":"2016-03-27T23:37:02","modified_gmt":"2016-03-27T23:37:02","slug":"handling-duplicate-query-string-arguments-express-js","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/handling-duplicate-query-string-arguments-express-js\/","title":{"rendered":"Handling duplicate query string arguments in Express.js"},"content":{"rendered":"<p>Duplicate query string entries are somewhat common, for example if you use Solr, you can do something like this:<\/p>\n<pre>\n\/execute?facet.field=stream_size&facet.field=content_type\n<\/pre>\n<p>If you make an Express.js endpoint, you get can this data through the &#8220;query&#8221; option:<\/p>\n<pre>\napp.use('\/execute\/', \n  function(req, res) {\n    console.log(req.query)\n  }\n)\n<\/pre>\n<p>The &#8220;req.query&#8221; is a Javascript object, which presents difficulties with duplicated arguments, which are joined into an array (so you do lose some information about order).<\/p>\n<p>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):<\/p>\n<pre lang=\"javascript\">\nlet args = req.query;\n\nlet queryString =\n  _.join(\n    _.keys(args)\n     .map( \n       (arg) => \n         isArray(args[arg]) ? \n         _.join(\n           args[arg].map( \n             (argv) => arg + '=' + argv \n           ), \n         '&') : \n         arg + '=' + args[arg]), \n    '&');\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Duplicate query string entries are somewhat common, for example if you use Solr, you can do something like this: \/execute?facet.field=stream_size&#038;facet.field=content_type If you make an Express.js endpoint, you get can this data through the &#8220;query&#8221; option: app.use(&#8216;\/execute\/&#8217;, function(req, res) { console.log(req.query) } ) The &#8220;req.query&#8221; is a Javascript object, which presents difficulties with duplicated arguments, which &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/handling-duplicate-query-string-arguments-express-js\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Handling duplicate query string arguments in Express.js&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[302,387,517],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3513"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=3513"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3513\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=3513"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=3513"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=3513"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}