What is a JavaScript proxy?
Why use it?
What features does it offer? This article tries to answer all those questions.
As browsers have become more complex, proxy developers have found themselves faced with increased demands in their day-to-day jobs. While proxies originally were used to intercept requests before they hit a web application, this was soon expanded to include the reverse situation as well - intercepting requests before they get sent to the server and modifying their content (see here for example). However proxies can be much more than that, offering developers many additional features.
In this article we will learn what a proxy is, why it is useful, its different types, and which feature makes a particular proxy better than the others. ? We can divide JavaScript proxies into two main categories: Browsers. Server proxies. The first type is the one you are probably most familiar with. Its goal is to intercept network traffic for a given set of pages/scripts in a browser and modify it according to the developers preferences or to improve security or reduce spam.
However, server proxies aren't just limited to web server installations. Most likely you already interacted with one during browsing sessions and never even realized it. The most common place where such a proxy is used is in a content delivery network (CDN). If your favorite site doesn't deliver fast enough for you, or isn't secure enough, then chances are you may already use a CDN, which operates on client devices and servers. A good proxy for the CDN is the very well know Cloudflare Reverse Proxy.
There's an ongoing debate between these two groups of proxy developers as to which is better. On one side we can see traditional browser proxies, on the other side server proxies. However, there are quite a few pros and cons to both. Here is what we came up with after analyzing each type and looking at many different pros and cons in both areas.
Pros and Cons of a Browser Proxy. The biggest and most obvious advantage of a browser proxy is that, due to being integrated into a browser and therefore already present in every client, is very lightweight. It's easy to set up and maintain, which enables developers to focus on core responsibilities of modern websites.
A browser proxy is a fully featured proxy, offering many advantages as we'll mention later.
How do you create a new proxy in JavaScript?
There are a number of proxy implementation libraries available, but they are all geared towards creating proxies for browser-hosted applications.
For creating proxies that run on servers, there's no reason to use a proxy library.
A proxy can be used to manipulate request headers, or to transform responses from an application. The following proxies are created in this blog post: HTTP header manipulator that takes an HTTP request and returns a modified request. HTTPS decryptor that modifies TLS requests to provide a new key and certificate. Request generator that runs in Node.js and creates requests to send to a remote server Server side proxy that sends responses back to the user. Header manipulator. This is the easiest proxy to create. It receives an HTTP request and adds some custom headers to it.
We will start with an HTTP interceptor. For now, this interceptor will just add the custom headers x-test: 1 and x-test: 2 to all requests. Later we will modify this proxy to provide different values for each request.
// src/interceptor.js const HttpInterceptor = ( request, response ) => } // src/api.js export // src/routes.js export default function ( ) } }
The following code starts the interceptor. A clone of the original request is created, and the headers that we want to add are added.
For requests sent to , this returns: A request sent to returns: GET / HTTP/1.1 Accept: application/json Host: www.com x-test: 2 Content-Type: application/json
To demonstrate how this works, we will also build a proxy that only responds with an error to requests with no x-test headers.
Related Answers
What is JavaScript extension?
I am working on an experimental project that uses JavaScript...
What is JavaScript executor in Selenium?
Currently we are automating our web applications us...
Are JavaScript proxies slow?
This was supported from Babel 7 onwards. b...