Creating An Effective Proxy Using Node And Express
Overview
Engineers are often faced with the challenge of pulling together multi-website/application projects, without full cross-platform permissions. Utilizing both a Node and an Express proxy, can pull together two websites/applications in a formal and cohesive format.
The aforementioned situation comes up more often than one would think, whether it be a question of host permissions or compatibility, it can undoubtedly pull up a number of possible roadblocks. There are several reasons a developer might not be able to run a site or app in their local environment; perhaps it’s complex to set up or involves a number of permissions. Regardless of the reason, Node and Express can present a novel way to solve the problem.
In Matt Billard’s Lightning Talk session, we will be uncovering the the primary strategies for Creating An Effective Proxy Using Node And Express:
The Architecture
The browser makes a request to the Node Express proxy server where the following three scenarios crop up:
In the example below, when using the InRhythm.com website as the target into which an engineer can inject some local code (in this case a basic Create React App project); the final result is an actual screenshot of the 2 websites living together in the same browser window.
How It Works
As mentioned above, website 1 and 2’s HTML are combined. This involves a few steps. Webpages can’t have 2 doctype, html, head, or body tags, so the use of some regex to strip those, will be required. Now that website 2’s HTML is ready, a coder can inject it before website 1’s closing </body> tag.?
The above code shows the modifications to website 1’s HTML.
It shows a few things:
"Gotchas" To Avoid
It can take a variety of trial and errors in order to develop a proxy to one's exact specifications. Some of the most common occurrences, a coder may find themselves troubleshooting are:
领英推荐
Live Demonstrations
Matt Billard has crafted an intuitive demonstration to help guide you through these principles in practicum:?
Be sure to follow Billard’s entire Lightning Talk to view this impressive demonstration in?real time.
Closing Thoughts
The Node.js framework Express allows an engineer to create web servers and APIs with minimal setup. Using Express in a Node.js application to create an API Proxy to request data from another API and return it to a consumer, is a vital skill to add to one's skills toolkit. Using Express middleware to help optimize the API Proxy, will allow a coder to raise the bar and improve performance for returning data from the underlying API.
To develop and learn from Billard's signature "Code Collider" proxy, feel free to download the direct code from GitHub.
Happy coding!
To learn more about Creating An Effective Proxy Using Node And Express, along with some live test samples, and to experience Matt Billard’s full Lightning Talk session, watch?here.
This article was originally published as a part of the Learning and Growth series on the InRhythm blog.?
This newsletter was curated by?Kaela Coppinger. Thoughts or questions? Sound off in the comments section below.