How to Build a Super Simple HTTP Proxy in JavaScript in just 20 lines of code

Oct 1, 2023 ยท 3 min read

It's super easy to build a basic proxy server with JavaScript. The trick lies in using the right modules from Node.js.

Today we will use the http module to handle incoming requests and the request module to fetch the target web pages.

First let's initialize and set the port:

const http = require('http');
const request = require('request');

const PORT = 9097;

Then we create a server that listens on the port and handles all incoming GET requests:

const server = http.createServer((req, res) => {
  if(req.method === 'GET') {
    handleRequest(req, res);
  }
});

server.listen(PORT);

The handleRequest function will fetch the target URL and pipe it back to the client:

function handleRequest(req, res) {
  const url = req.url.slice(1);

  req.pipe(request(url)).pipe(res);
}

We need to slice off the first slash from the URL path before making the request.

The key part is piping the request to the target URL and back to the response - this streams the content through without buffering everything in memory.

Finally we put it all together:

const http = require('http');
const request = require('request');

const PORT = 9097;

const server = http.createServer((req, res) => {
  if(req.method === 'GET') {
    handleRequest(req, res);
  }
});

function handleRequest(req, res) {
  const url = req.url.slice(1);

  req.pipe(request(url)).pipe(res);
}

server.listen(PORT);
console.log(`Proxy server running on port ${PORT}`);

Save this as proxy.js and run with node proxy.js.

You can test it by browsing to http://localhost:9097/>.

This gives you a basic proxy in just 20 lines of JS code. Of course for a production proxy there is a lot more you would want to add in terms of features, performance, reliability and security. But it's a simple starting point to understand the basics of piping HTTP requests.

This is great as a learning exercise but it is easy to see that even the proxy server itself is prone to get blocked as it uses a single IP. In this scenario where you may want a proxy that handles thousands of fetches every day using a professional rotating proxy service to rotate IPs is almost a must.

Otherwise, you tend to get IP blocked a lot by automatic location, usage, and bot detection algorithms.

Our rotating proxy server Proxies API provides a simple API that can solve all IP Blocking problems instantly.

  • With millions of high speed rotating proxies located all over the world,
  • With our automatic IP rotation
  • With our automatic User-Agent-String rotation (which simulates requests from different, valid web browsers and web browser versions)
  • With our automatic CAPTCHA solving technology,
  • Hundreds of our customers have successfully solved the headache of IP blocks with a simple API.

    The whole thing can be accessed by a simple API like below in any programming language.

    In fact, you don't even have to take the pain of loading Puppeteer as we render Javascript behind the scenes and you can just get the data and parse it any language like Node, Puppeteer or PHP or using any framework like Scrapy or Nutch. In all these cases you can just call the URL with render support like so:

    curl "<http://api.proxiesapi.com/?key=API_KEY&render=true&url=https://example.com>"
    

    We have a running offer of 1000 API calls completely free. Register and get your free API Key.

    Browse by tags:

    Browse by language:

    Tired of getting blocked while scraping the web?

    ProxiesAPI handles headless browsers and rotates proxies for you.
    Get access to 1,000 free API credits, no credit card required!