Mar 24th, 2023
How to Build a Super Simple HTTP proxy in Node JS in just 25 lines of code?

It's super easy to build a rudimentary reverse proxy server with Node JS.

Here's a step-by-step guide on creating a simple HTTP proxy server using Node.js:

Step 1: Set up the Project Create a new directory for your project and navigate into it in your terminal. Initialize a new Node.js project by running the following command:

npm init -y

Step 2: Install Dependencies In this project, we will use the http and https modules that come bundled with Node.js. There are no additional dependencies required. However, if you plan to use any additional libraries or frameworks, you can install them using npm install.


Step 3: Create a New JavaScript File Create a new JavaScript file in your project directory. You can name it whatever you like. In this example, let's name it simpleServer.js.


Step 4: Implement the Proxy Server Open the simpleServer.js file in your preferred code editor and paste the following code:

const http = require('http');

const https = require('https');

const url = require('url');

const PORT = 9097;

const server = http.createServer((clientReq, clientRes) => { const proxyUrl = url.parse(clientReq.url.substr(1));

const proxyReq = https.request({

host: proxyUrl.hostname,

path: proxyUrl.path,

method: clientReq.method,

headers: clientReq.headers

}, (proxyRes) => {

clientRes.writeHead(proxyRes.statusCode, proxyRes.headers);

proxyRes.pipe(clientRes);

});

clientReq.pipe(proxyReq);

});

server.listen(PORT, () => {

console.log(`Now serving at <http://localhost>:${PORT}`);

});


Here's a breakdown of the provided Node.js code:

Server Setup:

const server = http.createServer((clientReq, clientRes) => { // Code for handling client requests and proxying });


This creates an HTTP server using the http module. The server listens for incoming client requests.

Handling Client Requests and Proxying:

const proxyUrl = url.parse(clientReq.url.substr(1));


const proxyReq = https.request({

host: proxyUrl.hostname,

path: proxyUrl.path,

method: clientReq.method,

headers: clientReq.headers

}, (proxyRes) => {

clientRes.writeHead(proxyRes.statusCode, proxyRes.headers);

proxyRes.pipe(clientRes);

});

clientReq.pipe(proxyReq);


When a client request is received, the server parses the URL from the client request and creates a new HTTP request (proxyReq) to the target server using the https module. It sets the appropriate host, path, method, and headers based on the client request.

The response from the target server (proxyRes) is then piped directly to the client response (clientRes), effectively forwarding the response back to the client.

Server Listening:

server.listen(PORT, () => {

console.log(`Now serving at <http://localhost>:${PORT}`);

});

The server starts listening on the specified port (9097 in this case) and logs a message indicating that it is now serving at the specified URL.

Step 5: Run the Proxy Server Save the simpleServer.js file. In your terminal, navigate to the project directory and run the following command to start the proxy server:

node simpleServer.js

You should see a message indicating that the server is running at `http://localhost:9097`.

Step 6: Test the Proxy Server To test the proxy server, open your web browser and enter the following URL:

<http://localhost:9097/https://example.com>

You should see the response from the target website (`https://example.com`) displayed in your browser.

Alternatively, you can use cURL to make a request to the proxy server:

curl "<http://localhost:9097/https://example.com>"


The response from the target website will be displayed in your terminal.

Congratulations! You have successfully created a simple HTTP proxy server using Node.js.

Please note that this implementation is a basic example and may not handle certain scenarios or provide the same level of functionality as a full-fledged proxy server.

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](https://proxiesapi.com/blog/why-rotating-proxies-are-the-best-way-to-overcome-IP-Blocks.php) a lot by automatic location, usage, and bot detection algorithms.

Our rotating proxy server [Proxies API](https://proxiesapi.com/) 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 rotationWith 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 here.

Share this article:

Get our articles in your inbox

Dont miss our best tips/tricks/tutorials about Web Scraping
Only great content, we don’t share your email with third parties.
Icon