In the rapidly evolving field of web development, microservices are prevalent and their critical importance of high-performing and reliable proxy servers cannot be overstated. My previous exploration into Total.js, a robust framework for web applications, uncovered its powerful proxy creation capabilities. Since then, Total.js has released version 5, which brings significant updates and improvements to its proxy functionality. Today, I'm excited to delve into these updates, demonstrating how Total.js continues to ease and refine the development process.
Before diving into the latest updates, let's revisit why Total.js became my favorite tool for creating proxy servers. Its one-line setup, dependency-free nature, and ease of learning stood out amidst a sea of frameworks. Total.js not only promised but also delivered a lightweight, strong, real-time, and reliable solution for my proxy server needs.
Total.js v5 has introduced enhanced proxy functionalities, addressing bugs and expanding capabilities to offer more control and flexibility to developers. Here's what's new:
.check()
method allows developers to execute custom logic to determine if a request should be proxied. This adds an extra layer of control, enabling use cases like conditional proxying based on request properties..before()
and .after()
methods, developers can now manipulate requests before they're sent and responses before they're returned to the client. This is perfect for adding custom headers, logging, or any form of request/response transformation..remove()
method provides the ability to dynamically remove proxy routes, offering flexibility in managing proxy behaviors at runtime.The .copy()
method has been updated to support three modes of operation - none
, replace
, and extend
- providing finer control over how query strings are handled:
Upgrading your proxy server to leverage these new features is simple. Here's a quick guide to get you started:
Depending on your specific use case, you can choose how the proxy handles query strings:
.copy()
method in detailThe .copy()
method in Total.js v5 allows developers to specify how the proxy server handles the original request's query string when forwarding to the target URL. This method supports three distinct modes: none
, replace
, and extend
. Let's explore each mode with detailed examples to illustrate their use cases:
none
The none
mode tells the proxy to ignore the original query string entirely. This is useful when you want to proxy the request but don't need to pass any query parameters to the target URL.
Example:
GET /cl/?q=search
https://yourdomain.com/
In this mode, even if the original request contains query parameters (?q=search
), they are not forwarded to the target URL.
replace
The replace
mode is used when you want to replace the path of the original request with the proxy path, but without carrying over the query string.
Example:
GET /cl/?q=search
https://yourdomain.com/
GET /cl/something/?q=search
https://yourdomain.com/something/
In replace
mode, the path after the proxy endpoint (/cl/
) is appended to the target URL. However, the query string from the original request is not included.
extend
The extend
mode combines the path and query string from the original request and appends them to the target URL. This is particularly useful for APIs where you need to maintain the integrity of the original request.
Example:
GET /cl/?q=search
https://yourdomain.com/?q=search
GET /cl/something/?q=search
https://yourdomain.com/something/?q=search
Here, both the path and query parameters of the original request are preserved and appended to the target URL, making it an ideal choice for scenarios where every part of the request is essential for the recipient.
Selecting the appropriate mode depends on your specific use case:
none
when the target URL does not require any query parameters from the original request.replace
when you need to maintain the path but not the query parameters.extend
when both the path and query parameters are crucial for the target URL to process the request correctly.Understanding these modes and their applications ensures that you can tailor the proxy behavior to fit your application's needs precisely, enhancing both functionality and user experience.
Total.js v5 significantly expands the capabilities of its proxy functionality, providing developers with more tools and options to create efficient, flexible, and powerful proxy servers. Whether you're building microservices, handling complex routing, or simply need a proxy for development purposes, Total.js v5 offers the features and flexibility needed to get the job done.
Explore these new features and see how they can enhance your Total.js applications. Happy coding!