{"id":1796,"date":"2015-12-07T10:18:38","date_gmt":"2015-12-07T04:48:38","guid":{"rendered":"http:\/\/codeforgeek.com\/?p=1789"},"modified":"2022-11-21T13:53:24","modified_gmt":"2022-11-21T08:23:24","slug":"reverse-proxy-using-expressjs","status":"publish","type":"post","link":"https:\/\/codeforgeek.com\/reverse-proxy-using-expressjs\/","title":{"rendered":"Reverse proxy using ExpressJS"},"content":{"rendered":"<p>Reverse proxy is a proxy server which retrieve resources on behalf of client from one or more servers. Client end need not to know about all those servers. They request to proxy server on specific URL with over HTTP and proxy server finds out where to look ( in Servers ) to serve that request.<\/p>\n<p>In this tutorial we will learn and develop proxy server using Express.js. Our proxy server will :<\/p>\n<ul>\n<li>Contain 3 different server running on different port.<\/li>\n<li>Accept requests and return response.<\/li>\n<\/ul>\n<p><center><a class=\"button-rounded button-flat\" href=\"https:\/\/github.com\/codeforgeek\/node-proxy-server\" target=\"_blank\" rel=\"noopener\"><i class=\"fa fa-github\"><\/i>DOWNLOAD<\/a><\/center><\/p>\n<h3>About http-proxy :<\/h3>\n<p>Http-proxy is a node module developed by Nodejitsu, one of the leading Node.js hosting provider. This module will help us to write Reverse proxy in Node.js very easily.<\/p>\n<h3>Project installation :<\/h3>\n<p>Create new Node.js project and create package.json file. It is recommended to use <strong>npm init<\/strong> command to generate one.<\/p>\n<p>Once done use following command to install Express.js and http-proxy.<\/p>\n<p><code>npm install --save express http-proxy<\/code><\/p>\n<h3>Server code<\/h3>\n<p>In order to run our reverse proxy server we need some resource server from which Proxy will fetch data. In order to do so, let&#8217;s develop three Express server running on Port 3001,3002,3003 respectively.<\/p>\n<div class=\"file_name\">Server.js<\/div>\n<p><code lang=\"javascript\"><br \/>\nvar express = require(\"express\");<br \/>\nvar app = express();<\/code><\/p>\n<p><code lang=\"javascript\"><code lang=\"javascript\"><\/code><\/code><\/p>\n<p>app.get(&#8216;\/app1&#8217;,function(req,res) {<br \/>\nres.send(&#8220;Hello world From Server 1&#8221;);<br \/>\n});<\/p>\n<p><code lang=\"javascript\"><br \/>\n<\/code><\/p>\n<p><code lang=\"javascript\">app.listen(3001);<br \/>\n<\/code><\/p>\n<p>Copy paste same code for other servers too and change the text.<\/p>\n<h3>Proxy Server Code<\/h3>\n<p>Here is our simple proxy server code in express.js with multiple targets.<\/p>\n<div class=\"file_name\">app.js<\/div>\n<p><code lang=\"javascript\"><br \/>\nvar express  = require('express');<br \/>\nvar app      = express();<br \/>\nvar httpProxy = require('http-proxy');<br \/>\nvar apiProxy = httpProxy.createProxyServer();<br \/>\nvar serverOne = 'http:\/\/localhost:3001',<br \/>\nServerTwo = 'http:\/\/localhost:3002',<br \/>\nServerThree = 'http:\/\/localhost:3002';<\/code><\/p>\n<p><code lang=\"javascript\"><code lang=\"javascript\"><\/code><\/code><\/p>\n<p>app.all(&#8220;\/app1\/*&#8221;, function(req, res) {<br \/>\nconsole.log(&#8216;redirecting to Server1&#8217;);<br \/>\napiProxy.web(req, res, {target: serverOne});<br \/>\n});<\/p>\n<p><code lang=\"javascript\"><code lang=\"javascript\"><\/code><\/code><\/p>\n<p>app.all(&#8220;\/app2\/*&#8221;, function(req, res) {<br \/>\nconsole.log(&#8216;redirecting to Server2&#8217;);<br \/>\napiProxy.web(req, res, {target: ServerTwo});<br \/>\n});<\/p>\n<p><code lang=\"javascript\"><code lang=\"javascript\"><\/code><\/code><\/p>\n<p>app.all(&#8220;\/app3\/*&#8221;, function(req, res) {<br \/>\nconsole.log(&#8216;redirecting to Server3&#8217;);<br \/>\napiProxy.web(req, res, {target: ServerThree});<br \/>\n});<\/p>\n<p><code lang=\"javascript\"><br \/>\n<\/code><\/p>\n<p><code lang=\"javascript\">app.listen(3000);<br \/>\n<\/code><br \/>\nYou can add as many targets you want and it will create a proxy for that. In order to check whether its working or not we need to first run all the servers and hit request to \/app1 and \/app2 etc.<\/p>\n<h3>Running the app<\/h3>\n<p>Put all server in running mode, including proxy one. Note the servers are as follows :<\/p>\n<p>==&gt; localhost:3000 &#8211; Reverse proxy server.<br \/>\n== &gt;localhost:3001 &#8211; First resource server.<br \/>\n&#8230;<br \/>\n<a href=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2015\/12\/Screen-Shot-2015-12-06-at-12.17.20-am.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-1848\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2015\/12\/Screen-Shot-2015-12-06-at-12.17.20-am.png\" alt=\"Screen Shot 2015-12-06 at 12.17.20 am\" width=\"988\" height=\"716\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2015\/12\/Screen-Shot-2015-12-06-at-12.17.20-am.png 988w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2015\/12\/Screen-Shot-2015-12-06-at-12.17.20-am-768x557.png 768w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2015\/12\/Screen-Shot-2015-12-06-at-12.17.20-am-300x217.png 300w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2015\/12\/Screen-Shot-2015-12-06-at-12.17.20-am-640x464.png 640w\" sizes=\"(max-width: 988px) 100vw, 988px\" \/><\/a><\/p>\n<p>You can hit the other server target URL too and it will return a response from second server.<br \/>\n<a href=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2015\/12\/Screen-Shot-2015-12-06-at-12.18.04-am.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-1849\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2015\/12\/Screen-Shot-2015-12-06-at-12.18.04-am.png\" alt=\"Screen Shot 2015-12-06 at 12.18.04 am\" width=\"1202\" height=\"742\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2015\/12\/Screen-Shot-2015-12-06-at-12.18.04-am.png 1202w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2015\/12\/Screen-Shot-2015-12-06-at-12.18.04-am-768x474.png 768w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2015\/12\/Screen-Shot-2015-12-06-at-12.18.04-am-300x185.png 300w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2015\/12\/Screen-Shot-2015-12-06-at-12.18.04-am-1024x632.png 1024w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2015\/12\/Screen-Shot-2015-12-06-at-12.18.04-am-640x395.png 640w\" sizes=\"(max-width: 1202px) 100vw, 1202px\" \/><\/a><\/p>\n<h3>Conclusion :<\/h3>\n<p>Reverse proxy is one of the famous approach when it comes to security. Real world user will never know from which server resource came from. Developing reverse proxy from core in Node.js is little tricky but thanks to awesome contribution as http-proxy, our life is easier.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Reverse proxy is a proxy server which retrieve resources on behalf of client from one or more servers. Client end need not to know about all those servers. They request to proxy server on specific URL with over HTTP and proxy server finds out where to look ( in Servers ) to serve that request. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1851,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_surecart_dashboard_logo_width":"180px","_surecart_dashboard_show_logo":true,"_surecart_dashboard_navigation_orders":true,"_surecart_dashboard_navigation_invoices":true,"_surecart_dashboard_navigation_subscriptions":true,"_surecart_dashboard_navigation_downloads":true,"_surecart_dashboard_navigation_billing":true,"_surecart_dashboard_navigation_account":true,"_uag_custom_page_level_css":"","footnotes":""},"categories":[22,14,18],"tags":[],"class_list":["post-1796","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-express","category-nodejs","category-tutorial"],"blocksy_meta":[],"uagb_featured_image_src":{"full":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2015\/12\/banner.png",822,240,false],"thumbnail":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2015\/12\/banner-150x150.png",150,150,true],"medium":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2015\/12\/banner-300x88.png",300,88,true],"medium_large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2015\/12\/banner-768x224.png",768,224,true],"large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2015\/12\/banner.png",822,240,false],"1536x1536":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2015\/12\/banner.png",822,240,false],"2048x2048":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2015\/12\/banner.png",822,240,false]},"uagb_author_info":{"display_name":"Shahid","author_link":"https:\/\/codeforgeek.com\/author\/shahid\/"},"uagb_comment_info":0,"uagb_excerpt":"Reverse proxy is a proxy server which retrieve resources on behalf of client from one or more servers. Client end need not to know about all those servers. They request to proxy server on specific URL with over HTTP and proxy server finds out where to look ( in Servers ) to serve that request.&hellip;","_links":{"self":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/1796","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/comments?post=1796"}],"version-history":[{"count":0,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/1796\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media\/1851"}],"wp:attachment":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media?parent=1796"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/categories?post=1796"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/tags?post=1796"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}