
In Total.js v5, file routing is an efficient way to handle static files such as images, PDFs, and other documents. File routing is designed for performance and processes all file requests via the GET method. Whether you're serving static files or generating files dynamically (such as resizing images), file routing provides a streamlined and flexible approach in Total.js v5.
In this post, we’ll dive into how file routing works in Total.js v5, look at examples, and briefly highlight the key differences from Documentation.
File routing in Total.js v5 allows you to define routes for serving or dynamically processing files. These routes can handle specific file types or all files within a directory. This is especially useful for serving images, documents, and other static content efficiently.
Here’s how to define routes for handling static files like documents and images:
ROUTE('FILE /documents/*.*'): This handles all file types in the /documents folder.ROUTE('FILE /images/*.jpg'): This handles only .jpg files in the /images folder.$.file(): This method serves the requested file.One of the powerful features of file routing is the ability to dynamically process files before serving them. For example, you can resize images based on URL parameters or requests.
$.image(): This method allows you to manipulate images (resize, change quality, etc.) before sending them to the client.In Total.js v5, file uploads are handled with the POST method using the @upload flag. You can define routes that handle file uploads with size restrictions and process the uploaded files easily.
ROUTE('POST /upload/ @upload <1MB'): Defines a route for uploading files with a 1MB limit.$.files: Contains an array of the uploaded files.$.files.While Total.js v5 builds on the same foundation as v4, there are some key improvements in how file routing and handling are structured:
$ Object: In v5, the $ object replaces the separate req and res objects from v4. This simplifies how you handle file routing, requests, and responses by merging them into a single object.$.image() method, allowing direct manipulation of images within the routing logic.File routing in Total.js v5 is powerful, efficient, and flexible, making it easy to serve static files and handle dynamic content such as image resizing. Whether you're managing simple document requests or processing images on the fly, file routing provides the tools to handle static content effectively.
Here’s a summary of what we covered:
POST method and the @upload flag.$ object and simplified image processing.By using these features, you can build more dynamic and responsive applications with Total.js v5.