The client-server model is the interaction between the server and the client. We know that a client typically requests a resource from a server, and the server responds to that request. Do you know that Reverse Proxy Server has emerged to be a crucial component in modern web development? It can improve the performance and reliability of the system while defending web servers from threats.
So if you want to develop an express application and set up a reverse proxy, you have arrived at the right destination. In this article, we will discuss Reverse Proxy for Express. We will also discuss what exactly reverse proxy is, and its key features. After properly understating the concepts, we will see how to set up a reverse proxy server that handles the incoming requests for an express application.
About Express.js
Programmers use Express.js due to its simplicity and wholesome features. You can use the effective routing system provided by express.js to build your web application and define routes.
Express.js is a Node.js framework that simplifies the development of web applications. It provides various features and tools such as routing, middleware, templating engines, error handling, static file serving, request, and response, etc.
Node.js is a runtime environment for implementing your javascript code on the server side, while Express is a Node.js framework. Express uses the already existing features of Node.js and enhances it further, providing a streamlined approach for developing your web applications.
To install Express, you need to run the following command in your terminal.
npm install express
Or
npm i express
NPM stands for Node Package Manager. It manages and distributes the javascript libraries and packages. Using it, we can install, manage and update dependencies of our application.
The Client-Server Model tells us how the client requests information/resources from the server, and the server responds to these requests.
Let's understand reverse proxy servers through an example. Let's say you are building a web application that consists of multiple back-end services which handle different features of your application like processing data, item delivery, and other factors. So, here proxy server comes into the picture, where it can distribute various requests from a client across the back-end servers.
You may use a reverse proxy server that would sit in between the client and various back-end servers, forwarding the requests from the client to the servers and, in turn, responding back to the client.
Therefore, rather than directly interacting with the web server, the client first interacts with the Reverse Proxy server. The reverse proxy server then sends the request to the main web server and gets backs the response, and further sends it back to the client.
Uses of Reverse Proxy
Below are some of the uses of Reverse Proxy in modern web development:
The reverse proxy plays a major role in load balancing, where the requests from the client are distributed across various servers.
Using the reverse proxy server, you can cache the content that can be further used in other client requests.
A reverse proxy acts as a gateway through which you can access APIs.
It also provides security from malicious activities. It filters the suspicious traffic, thus protecting the server.
Implementing Reverse Proxy for Express
Let's implement a Reverse Proxy for Express.
Prerequisites
You need to make sure you have the following installed in your system.
Once you have all the necessary software installed, let's build Reverse Proxy for Express.
To implement the functionality of the reverse proxy server, we will set up two servers using Express. The proxy server will fetch data from these two and will display it on the client side.
Step 1: Initialise npm
As a first step, you need to mention the path in the terminal where you want to store your application and then initialize npm using the following command.
cd FileName
npm init
For example,
Step 2: Install the express
Next, we need to install the express dependencies by running the following command.
npm i express
Step 3: Server Code
Next, we need to create a file name ‘app.js’ and write the required server code.
var express = require("express");
var app = express();
app.get('/ninja-app1', function(req,res){
res.send("Hello Ninja from server 1");
})
app.listen(4000, function() {
console.log("Server started on port 4000");
});
In the above code, we have set up a server that listens on port 4000.
Step 4: Run the server
Run the server using the following command in the terminal.
node app.js
Similarly, you can repeat the above four steps and run another server at port 4001. Just change the path ‘app.get’ and ‘listen’ on port 4001.
Reverse-Proxy Server Code
We have made a separate file named‘NinjaProxyServer’. Here also we need to initialize npm as discussed above.
To create a Proxy Server, we need to install the http-proxy module. The command for installing the express and http-proxy modules are mentioned below.
npm i express http-proxy
Code
Next, we need to write the following code as mentioned below in our ‘app.js’ file and define the specific route handlers for paths on both servers.
var express = require("express");
var app = express();
var httpProxy = require('http-proxy');
// For using proxy server objects
var ApiProxy=httpProxy.createProxyServer();
var serverOne='http://localhost:4000';
var serverTwo='http://localhost:4001';
// Proxy Server
app.all('/', function(req,res){
res.send("Hey Ninja! This is Proxy server");
})
// Proxy server routing to the target Server 1
app.all('/ninja-app1', function(req,res){
console.log("Hey Ninja! Redirecting to Server1");
ApiProxy.web(req,res,{target:serverOne});
})
// Proxy server routing to the target Server 1
app.all('/ninja-app2/', function(req,res){
console.log("Hey Ninja! Redirecting to Server2");
ApiProxy.web(req,res,{target:serverTwo});
})
app.listen(2000,function(){
console.log("server running at port 2000");
})
Proxy Server at localhost:2000→
Redirecting to server1→
In the terminal, you can see that the message server redirects to ‘server1’ when we request the path ‘/ninja-app1’.
Redirecting to server2→
In the terminal, you can see that the message server redirects to ‘server2’ when we request for the path ‘/ninja-app2’.
Explanation
We have created two servers running on ports 4000 and 4001, and the proxy server runs on port 2000. In the above code, we have defined specific route handlers for paths:
/ninja-app1 (from server 1)
/ninja-app2 (from server 2)
So our proxy server forwards requests for these defined paths to Server1 and Server2 and, in response, gives us the information from the respective servers back to the client.
Note: Make sure that both servers are running on their respective ports while accessing their respective paths (i.e. Server1 at localhost:4000 and server2 at localhost:4001).
Features of HTTP-proxy module
Below are some of the key features of a proxy server:
Proxying and Routing: The HTTP-proxy module provides us with efficient routing techniques using which the client’s requests are handled. It takes into account various parameters such as headers, hostname, and URL.
Security: The module also provides various security features and blocks suspicious traffic, thus protecting the server from malicious attacks.
Load Balancing: The HTTP module can distribute client requests among multiple backend servers. For example, above, we have created two servers, and the client requests from the proxy server, which in turn distributes the requests between both servers based on the request type.
Dynamic Routing: The HTTP-proxy module supports Dynamic routing. It means that the rules of routing can be updated at runtime.
Web Socket: The http-proxy module supports WebSocket connections. WebSocket is a communication protocol that enables bi-directional communication.
Frequently Asked Questions
What is HTTP-Proxy?
We need to install the http-proxymodule to create a Reverse Proxy Server. Using this module, you can create a proxy server that sits in between the client and the server. It forwards the request from the client to the specific server and then responds to the client. To install the module, you need to run the following command in your system’s terminal: ‘npm i http-proxy’.
What is Reverse Proxy Server?
You can use the reverse proxy server that would sit in between the client and various back-end servers, forwarding the requests from the client to the servers and in turn, responding back to the client. They help in increasing security, performance, and reliability.
What is the difference between Express and Node.js?
The main difference between the two is that Node.js provides a runtime environment to implement the javascript on the back-end. At the same time, Express.js is a framework of node.js that is mainly used in simplifying the development process by providing various sets of tools.
What are the features of the HTTP-proxy module?
The HTTP-proxy module offers various features such as Dynamic Routing, Web socket connections, Proxying and routing, Security, load balancing, etc.
Conclusion
In this article, we have learned about Reverse Proxy for Express. We have seen how we can implement reverse proxy in the express and the features offered by the implementation. To learn more about back-end development, refer to the articles below and take your back-end development journey to the next level.
You can read more such descriptive articles on our platform, Coding Ninjas Studio. You will find straightforward explanations of almost every topic on the platform. So take your coding journey to the next level using Coding Ninjas.