### Install serve-handler with Yarn Source: https://github.com/vercel/serve-handler/blob/main/README.md Install the serve-handler package using Yarn. This is the first step to integrating it into your project. ```sh yarn add serve-handler ``` -------------------------------- ### Basic Usage with Micro Source: https://github.com/vercel/serve-handler/blob/main/README.md Integrate serve-handler into a Node.js HTTP server using the 'micro' framework. This example shows the minimal setup required to handle requests. ```js const handler = require('serve-handler'); module.exports = async (request, response) => { await handler(request, response); }; ``` -------------------------------- ### Install serve-handler with npm Source: https://github.com/vercel/serve-handler/blob/main/README.md Install the serve-handler package using npm. This is an alternative to using Yarn for package management. ```sh npm install serve-handler ``` -------------------------------- ### Customizing serve-handler with Options Source: https://github.com/vercel/serve-handler/blob/main/README.md Configure serve-handler's behavior by passing an options object as the third argument. This example demonstrates enabling clean URLs. ```js await handler(request, response, { cleanUrls: true }); ``` -------------------------------- ### Configure Directory Listing Restrictions Source: https://github.com/vercel/serve-handler/blob/main/README.md Specify which directory paths should have directory listing enabled using glob patterns. Paths starting with '!' are excluded. ```json { "directoryListing": [ "/assets/**", "/!assets/private" ] } ``` -------------------------------- ### Exclude Files from Directory Listing Source: https://github.com/vercel/serve-handler/blob/main/README.md Define a list of files or directories to exclude from directory listings. Common examples include hidden files and version control directories. ```json { "unlisted": [ ".DS_Store", ".git" ] } ``` -------------------------------- ### Configure Public Directory Source: https://github.com/vercel/serve-handler/blob/main/README.md Specify a custom directory to serve files from. This is useful for projects with a specific build output directory, like Jekyll. ```json { "public": "_site" } ``` -------------------------------- ### Configure Redirects Source: https://github.com/vercel/serve-handler/blob/main/README.md Set up redirects to forward requests from one path to another, optionally specifying the HTTP status code. Supports glob patterns. ```json { "redirects": [ { "source": "/from", "destination": "/to" }, { "source": "/old-pages/**", "destination": "/home" } ] } ``` -------------------------------- ### Configure Public Directory with Absolute Path Source: https://github.com/vercel/serve-handler/blob/main/README.md Use an absolute path to define the directory from which files should be served. This ensures consistency regardless of the current working directory. ```json { "public": "/path/to/your/_site" } ``` -------------------------------- ### Enable Single File Rendering Source: https://github.com/vercel/serve-handler/blob/main/README.md Enable `renderSingle` to automatically render a file if a directory contains only one file. This is useful for non-HTML files. ```json { "renderSingle": true } ``` -------------------------------- ### Custom Middleware for File System Operations Source: https://github.com/vercel/serve-handler/blob/main/README.md Replace default file system interaction methods by providing custom middleware functions. Ensure all arguments are passed to native calls for methods like `createReadStream`. ```javascript await handler(request, response, undefined, { lstat(path) {}, realpath(path) {}, createReadStream(path, config) {} readdir(path) {}, sendError(absolutePath, response, acceptsJSON, root, handlers, config, error) {} }); ``` -------------------------------- ### Configure Rewrites with Routing Segments Source: https://github.com/vercel/serve-handler/blob/main/README.md Utilize routing segments in the source path to dynamically map to destination files, passing parameters like IDs. ```json { "rewrites": [ { "source": "/projects/:id/edit", "destination": "/edit-project-:id.html" }, ] } ``` -------------------------------- ### Set Custom Cache-Control Headers Source: https://github.com/vercel/serve-handler/blob/main/README.md Configure custom headers, such as Cache-Control, for specific file types or paths using glob patterns. ```json { "headers": [ { "source" : "**/*.@(jpg|jpeg|gif|png)", "headers" : [{ "key" : "Cache-Control", "value" : "max-age=7200" }] }, { "source" : "404.html", "headers" : [{ "key" : "Cache-Control", "value" : "max-age=300" }] } ] } ``` -------------------------------- ### Enable Symlink Resolution Source: https://github.com/vercel/serve-handler/blob/main/README.md Enable `symlinks` to resolve symlinks to their targets. By default, symlinks are disabled for security and treated as non-existent. ```json { "symlinks": true } ``` -------------------------------- ### Configure Redirects with Custom Status Codes and Routing Segments Source: https://github.com/vercel/serve-handler/blob/main/README.md Redirect requests with specific HTTP status codes (e.g., 302) and use routing segments for dynamic path mapping. ```json { "redirects": [ { "source": "/old-docs/:id", "destination": "/new-docs/:id" }, { "source": "/old", "destination": "/new", "type": 302 } ] } ``` -------------------------------- ### Enable ETag Header Source: https://github.com/vercel/serve-handler/blob/main/README.md Enable `etag` to send a strong ETag header instead of a Last-Modified header. This is disabled by default due to potential performance costs for large files. ```json { "etag": true } ``` -------------------------------- ### Configure Rewrites for SPAs Source: https://github.com/vercel/serve-handler/blob/main/README.md Use rewrites to serve a different file for a given path, ideal for single-page applications. Supports glob patterns and routing segments. ```json { "rewrites": [ { "source": "app/**", "destination": "/index.html" }, { "source": "projects/*/edit", "destination": "/edit-project.html" } ] } ``` -------------------------------- ### Disable Clean URLs Source: https://github.com/vercel/serve-handler/blob/main/README.md Turn off the feature that automatically removes the '.html' extension from file paths. Use this if you need to preserve extensions. ```json { "cleanUrls": false } ``` -------------------------------- ### Force Trailing Slashes on URLs Source: https://github.com/vercel/serve-handler/blob/main/README.md Configure the handler to enforce trailing slashes on all URLs, resulting in a 301 redirect for URLs without them. ```javascript { "trailingSlash": true } ``` -------------------------------- ### Restrict Clean URLs to Specific Paths Source: https://github.com/vercel/serve-handler/blob/main/README.md Apply the clean URLs feature only to paths matching the provided globs. This allows for selective URL rewriting. ```json { "cleanUrls": [ "/app/**", "/!components/**" ] } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.