How to easily build a Node.JS HTTP Proxy Server

Daniel - September 15, 2020

How to easily build a Node.JS HTTP Proxy Server

Tutorials

Today, we are going to show you how to build a Node.JS proxy server which can facilitate both HTTPS and HTTP requests without hassles. Also, we will show you how whitelist specific IP addresses as well as how to blacklist certain websites on the proxy server.

Nevertheless, novice programmers will find this tutorial helpful and easy to apply in deploying their own Node.JS proxy on their system which can then be configured on Mozilla Firefox web browser.

Prerequisites

Check out our full range of Residential Proxies

The following are necessary requirements in order to build the HTTP proxy server:

Find the perfect Proxy Product.

Proxyrack offers a multiple options to suit most use cases, if you are unsure our 3 Day Trial allows you to test them all.

Security

Residential proxies

Never get blocked, choose your location

View all option available
Vault

Datacenter proxies

Super fast and reliable

View all option available
Try

3 Day Trial

Test all products to find the best fit

View all option available
  • Node.JS Framework

  • Web Browser (Mozilla Firefox or any other standard web browser)

  • Basic JavaScript (or coding knowledge)

  • Notepad program (or any other text editor)

  • Ability to follow instructions

However, before we proceed let’s find out what exactly is an HTTP proxy server and how does it work.

What is HTTP Proxy Server? How does it work?

An HTTP proxy server is a computer which acts as an intermediary between the client and other server (destination) in order to handle HTTP requests over the LAN or the internet. In other words, the HTTP proxy server accepts HTTP protocol requests from the client (web browser), checks the cache, and if possible generates resources requested for by the client from various servers.

On the other hand, proxy server can cache series of electronic resources such as webpages, images, documents, and others. Hence, users can access their desired resources while reducing network traffic, web server loads, and other constraints associated with handling HTTP requests directly.

Web proxies provide a large variety of benefits for millions of users across the globe. Here are some of the benefits of web proxies:

  • Expands online security

  • Facilitates online anonymity

  • Blocks malicious traffic

  • Bypass geo-restrictions online

  • Improve loading times

  • Mask your online identity

  • Access to censored contents, and more

Nonetheless, for the sake of this tutorial Node.JS is required to build your own kind of HTTP Proxy server.

What is Node.JS? How do I install it?

Node.JS is JavaScript runtime which can run in any browser’s JavaScript Engine for building scalable network applications. Unlike other programming languages, Node.JS is an interpreter for a JavaScript running environment. Hence, Node.JS is usually viewed as a JavaScript framework due to its JavaScript dependency on server.

One of the notable features of the Node.JS is its cross-compatibility, i.e. it can run on various platforms such as Windows, Mac OS, Unix, Linux, and more. Also, Node.JS utilizes an event-driven non-blocking I/O model which makes it efficient and lightweight for running real-time applications across diverse devices.

Why did we choose Node.JS?

Simple!

Node.JS is notable for its easy approach to processing requests due to its asynchronous nature. Also, it comes with a pre-existing proxy package that called http-proxy-server.

Now, let’s move to installing Node.JS if you don’t have it already on your PC.

Node.JS is one of the heavily supported programming languages which is frequently updated; hence, new versions are readily available over short period of time. As at the time of this writing, the current version of Node.JS is 12.11.1.

Note: Node.JS is compatible with Windows, Linux, Mac OS, SmartOS, etc.

Follow through these steps to install Node.JS:

  • Go to the official Node.JS website.

  • Click on the Download Current version option.

  • After downloading the installer file, and then double-click on it to commence Node.js installation.

  • The installer will open a new window prompting you to accept the licensing agreements. Follow the prompts to finish the installation.

https://imgur.com/a/6IxC4VL

Note: While installing Node.JS, you also get npm (external libraries of the Node.JS ecosystem) as well as other components such as Chocolatey, Visual Studio Installer, Python, etc. Also, don’t be alarmed when your PC is automatically updated to the latest version.

Alternatively, if you already have Node.JS installed on your PC, you can update it before you proceed.

Hence, you can either use the installer file or make use of the Node Package Manager (npm).

How to create a basic Proxy in Node.JS

In order to create the basic proxy server in Node.JS, follow these steps:

  • Launch your text editor (preferably Notepad++).

  • Copy the code below and save it as proxy.js in C:\\Users\Your Name\

var http = require('http');

var proxy = require('http-proxy');

proxyServer= proxy.createProxyServer({target:'http://127.0.0.1:9000'});

proxyServer.listen(8000);

server = http.createServer(function (req, res) {

  res.writeHead(200, { 'Content-Type': 'text/plain' });

  res.write('Proxy Request was Successful!' + '\n' + JSON.stringify(req.headers, true, 2));

  res.end();

});

server.listen(9000);
  • Use the following command to run the file.

node proxy.js

This will create the working basic proxy server.

To view the contents of any other http website, we need to modify our code below:

var http = require('http');

var proxy = require('http-proxy');

var url = require('url');

proxyServer = proxy.createProxyServer({target:'http://127.0.0.1:9000'});

proxyServer.listen(8000);

server = http.createServer(function (req, res) {

 console.log(req.url);

 proxyServer.web(req, res, { target: req.url });

 proxyServer.on('error', function(e) {

  console.log("Error in proxy call");

 });

});

server.listen(9000);

  • Run the script afterwards.

How to configure your web browser

Basically, you need to configure your web browser to facilitate proxy connections.

Note: The process of adding proxy server for other web browser varies. However, for the purpose of this tutorial, we will only be showing you how to add proxy server for Mozilla Firefox.

Here’s how to do this:

  • Launch Mozilla Firefox

  • Go to Menu > Options

https://imgur.com/a/GtWBBi2

  • Scroll down to the Network Settings and click on it

https://imgur.com/vCcGTgs

  • Check the Manual Proxy tick box and then enter your proxy server information i.e. HTTP Proxy and Port. Use http://127.0.0.1/ for HTTP Proxy and 9000 for Port. Note: this information is based on the basic proxy configuration information.

https://imgur.com/4HGROz8

  • Now, tick on the checkbox for “Use this proxy server for all protocols”. This will enable HTTPS connection (we will cover this below).

How to add IP Address to IP list (Whitelist)

Basically, the proxy server will not allow strange IP connections; hence, it is necessary that you add all the IP address you want the proxy server to be accessible to – from the iplist file.

  • Enter the following command to do this:

$ echo '1.2.3.4' >> iplist

Note: Replace 1.2.3.4 with your computer’s IP address. Repeat the same process for all your authorized IP addresses.

How to blacklist a website on your proxy server

One of the objectives of creating proxy server is to control access to the internet within an environment most especially a business environment. If you do not want your employee to visit certain websites whether illegal or provocative while using your office computers, you may have such websites.

Nevertheless, you can also use your Node.JS HTTP proxy server to blacklist such websites. This process requires complementing our proxy server with custom logic.

To do this, simply:

  • Open your favourite text editor and add the URLs for all the sites you want to block.

  • Then, add the preceding code to your proxy server.

var blocked = [];

fs.watchFile('./blocked.txt', function(c,p) { update_blocked_list(); });

function update_blocked_list() {

  sys.log("Updating blocked files.");

  blocked = fs.readFileSync('./blocked.txt').split('\n')

              .filter(function(rx) { return rx.length })

              .map(function(rx) { return RegExp(rx) });

}

http.createServer(function(request, response) {

  for (i in blocked) {

    if (blocked[i].test(request.url)) {

      sys.log("Denied: " + request.method + " " + request.url);

      response.end();

      return;

    }

  }

}

  • Run the file afterwards and then check to see if you can access any of the blocked URLS on your web browser.

Usually, the access denied prompt comes up when you make such request.

How to enable HTTPS on your proxy server

Several websites nowadays are HTTP secured i.e traffic to such websites are encrypted. Although the aim of this tutorial is to create an HTTP proxy server, we will also show you how to enable HTTPS on your proxy server. This will eliminate the constraints of accessing only HTTP websites via your proxy server.

Afterwards, you need to implement the server to serve https requests by entering this command:

var fs = require('fs');

var http = require('http'),

var https = require('https'),

var httpProxy = require('http-proxy');

isHttps = true;

var options = {

  ssl: {

    key: fs.readFileSync('valid-key.pem'),

    cert: fs.readFileSync('valid-cert.pem')

  }

};

proxyServer = proxy.createProxyServer({target:'http://127.0.0.1:9000'});

proxyServer.listen(9000);

if (isHttps){

   server = https.createServer(options.ssl, function(req, res) {

     console.log(“https request”);

     proxyServer.web(req, res, { target: req.url });

     proxyServer.on('error', function(e) {

  console.log("Error in proxy call");

     });

     proxyServer.listen(9000);

   });

}else{

  server = http.createServer(function (req, res) {

  console.log(req.url);

  proxyServer.web(req, res, { target: req.url });

  proxyServer.on('error', function(e) {

   console.log("Error in proxy call");

  });

    });

 }

 server.listen(9000);

Conclusion

Check out our full range of Residential Proxies

In this tutorial, we have walked you through the process of creating a basic HTTP/HTTPS proxy server using Node.JS. At the end of this article, you should be able to install/upgrade Node.JS runtime engine. Also, you should be able to whitelist your IP address(es) for easy access to the proxy server or blacklist certain websites efficiently.

Nevertheless, if the process of building the HTTP proxy server scares you, you can subscribe to any of our residential proxies or ProxyRack VPN for guaranteed online anonymity.

Share your Node.JS HTTP proxy server building experience with us by commenting below.

Find the perfect Proxy Product.

Proxyrack offers a multiple options to suit most use cases, if you are unsure our 3 Day Trial allows you to test them all.

Security

Residential proxies

Never get blocked, choose your location

View all option available
Vault

Datacenter proxies

Super fast and reliable

View all option available
Try

3 Day Trial

Test all products to find the best fit

View all option available

Related articles

Daniel - May 9, 2022

How To Create A Custom SEO Tool

Tutorials
Read Article

Daniel - May 9, 2022

Best Proxies For Enterprises

Tutorials
Read Article

Daniel - April 7, 2022

What Is Autofill Magic?

Tutorials
Read Article

Daniel - April 1, 2022

What Is AdsPower Browser?

Tutorials
Read Article

Get Started by signing up for a Proxy Product

View Plans