{"id":796,"date":"2014-03-09T00:12:52","date_gmt":"2014-03-09T00:12:52","guid":{"rendered":"http:\/\/tech.avant.net\/q\/?p=796"},"modified":"2019-06-01T02:02:09","modified_gmt":"2019-06-01T02:02:09","slug":"node-js-redirect-with-query-string","status":"publish","type":"post","link":"https:\/\/tech.avant.net\/q\/node-js-redirect-with-query-string\/","title":{"rendered":"node.js redirect with query string"},"content":{"rendered":"<p>Previously, I discussed <a href=\"\/q\/2014\/03\/javascript-appending-to-query-string\/\">javascript appending to query string<\/a>, where we serialized an associative array to a query string. I would now like to leverage this technique within <a href=\"http:\/\/nodejs.org\/\" target=\"_blank\" rel=\"noopener noreferrer\">node.js<\/a> as a redirect service.<\/p>\n<p>Specifically, I am using <a href=\"http:\/\/expressjs.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">express<\/a> to make a web app in node.js and the app includes a redirect service, e.g.,<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">var app = express();\nvar redirectVars = {'foo':'spam and eggs', 'tracker':42 };\n\n\/\/ redirect and append redirectVars\napp.get('\/redirect', function(request, result, next) {\n  if(request.query.url) {\n    var urle = request.query.url;\n    var url = decodeURIComponent(urle);\n    var firstSeperator = (url.indexOf('?')==-1 ? '?' : '&amp;');\n\n    var queryStringParts = new Array();\n    for(var key in redirectVars) {\n      queryStringParts.push(key + '=' + encodeURIComponent(redirectVars[key]));\n    }\n    var queryString = queryStringParts.join('&amp;');\n\n    result.redirect(url + firstSeperator + queryString);\n  }\n  result.send(\"400\", \"Bad request\");\n});<\/pre>\n<p>Usage of this service is as simple as,<\/p>\n<pre>\/redirect?url=new-location\n<\/pre>\n<p>Any external app could use this service, which will append server controlled query string variables to the redirected URL. This is useful for a redirect service that needs to dynamically construct query string variables, such as cross-domain authentication and authorization.<\/p>\n<p>Importantly, in order to preserve an existing query string in the new-location, simply encode the entire URL string before sending it into the service, e.g.,<\/p>\n<pre class=\"sh_javascript\">var new_location = encodeURIComponent(\"http:\/\/foo.com\/?q=test\");\nwindow.location = \"http:\/\/www.yourapp.com\/redirect?url=\" + new_location;\n<\/pre>\n<p>Using the above node.js example, this would have the effect of redirecting the user to<\/p>\n<pre>http:\/\/foo.com\/?q=test&amp;foo=spam%20and%20eggs&amp;tracker=42<\/pre>\n<hr>\n<div id=\"amzn-assoc-ad-aefa7676-1e81-45ee-9f86-a6899fe69243\"><\/div>\n<p><script async=\"\" src=\"\/\/z-na.amazon-adsystem.com\/widgets\/onejs?MarketPlace=US&amp;adInstanceId=aefa7676-1e81-45ee-9f86-a6899fe69243\"><\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Previously, I discussed javascript appending to query string, where we serialized an associative array to a query string. I would now like to leverage this technique within node.js as a redirect service. Specifically, I am using express to make a web app in node.js and the app includes a redirect service, e.g., var app = [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[9],"tags":[],"_links":{"self":[{"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/posts\/796"}],"collection":[{"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/comments?post=796"}],"version-history":[{"count":10,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/posts\/796\/revisions"}],"predecessor-version":[{"id":991,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/posts\/796\/revisions\/991"}],"wp:attachment":[{"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/media?parent=796"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/categories?post=796"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/tags?post=796"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}