Proxy Service
Make an Express proxy server that proxies to a Flask server.
We'll cover the following
Setting up the proxy server
Since the development web server built into the Parcel bundler does not currently support acting as a proxy for another web service such as Flask on its own, we need to utilize a web server that can support that capability. The Express.js web framework for Node.js fits this requirement nicely and is very lightweight. Along with that, we will also utilize the http-proxy-middleware JavaScript library, which lets us cleanly integrate with the Parcel web server.
Most of the time, our back-end web server will have some kind of a RESTful API that accepts a request from a web client and responds by sending back JSON data. One assumption we will make is that the request URL for the back-end REST service has an endpoint that can be differentiated from requests for the front-end via a regex filter. For example, it’s common for a REST API to have a URL that starts with /api/
, whereas requests going to the front-end web server start with something else besides that. The proxy middleware can use this pattern to know when to respond to the request locally, and when to forward it onto the back-end web server.
Next, we need to have a script to do the routing for the proxy service. Since it runs in Node.js, it will have to be a JavaScript file, but it’s pretty straightforward:
Get hands-on with 1400+ tech skills courses.