or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

index.mddocs/

connect-slashes

connect-slashes is a trailing slash redirect middleware for Connect and Express.js applications. It provides canonical URL handling by consistently appending or removing trailing slashes from request URLs, helping with SEO optimization and URL consistency in Node.js web applications.

Package Information

  • Package Name: connect-slashes
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install connect-slashes

Core Imports

const slashes = require("connect-slashes");

For ES Modules (when using with modern bundlers):

import slashes from "connect-slashes";

Basic Usage

const connect = require("connect");
const slashes = require("connect-slashes");

// Append trailing slashes (default behavior)
connect()
  .use(connect.static())
  .use(slashes()) // must come after static middleware!
  .listen(3000);

// Remove trailing slashes
connect()
  .use(connect.static())
  .use(slashes(false))
  .listen(3000);

For Express.js:

const express = require("express");
const slashes = require("connect-slashes");

const app = express();

app.use(express.static("public"));
app.use(slashes()); // append trailing slashes

app.listen(3000);

Capabilities

Middleware Factory Function

Creates a Connect/Express middleware function that handles trailing slash redirects.

/**
 * Creates trailing slash redirect middleware
 * @param {boolean} append - Whether to append trailing slashes (default: true)
 * @param {SlashesOptions} options - Configuration options
 * @returns {Function} Connect/Express middleware function
 */
function slashes(append, options);

interface SlashesOptions {
  /** HTTP status code for redirects (default: 301) */
  code?: number;
  /** Base path to prepend to redirect URLs (default: "") */
  base?: string;
  /** Additional HTTP headers for redirect responses (default: {}) */
  headers?: { [key: string]: string };
}

Parameters:

  • append (boolean, optional): Controls slash behavior
    • true (default): Appends trailing slashes to URLs that don't have them
    • false: Removes trailing slashes from URLs that have them
  • options (object, optional): Configuration object with the following properties:
    • code (number): HTTP status code for redirects (default: 301 Moved Permanently)
    • base (string): Base path to prepend to redirect URLs, useful for reverse proxy scenarios
    • headers (object): Additional HTTP headers to include in redirect responses

Returns: A middleware function with signature (req, res, next) => void

Usage Examples:

// Basic usage - append trailing slashes with 301 redirects
app.use(slashes());

// Remove trailing slashes
app.use(slashes(false));

// Custom redirect status code
app.use(slashes(true, { code: 302 }));

// Add base path for reverse proxy scenarios
app.use(slashes(true, { base: "/blog" }));

// Include custom headers in redirect responses
app.use(slashes(true, { 
  headers: { "Cache-Control": "public, max-age=3600" } 
}));

// Combined options
app.use(slashes(true, {
  code: 302,
  base: "/api",
  headers: { "X-Redirect-Reason": "trailing-slash" }
}));

Middleware Function Behavior

The returned middleware function processes HTTP requests with the following behavior:

  • Method Filtering: Only processes GET, HEAD, and OPTIONS requests to avoid losing POST/PUT data
  • URL Processing: Uses req.originalUrl when available (Express compatibility), falls back to req.url
  • Query Parameter Preservation: Maintains all query parameters in redirect URLs
  • Double Slash Cleanup: Automatically removes consecutive slashes (e.g., //foo becomes /foo/)
  • Absolute URL Guarantee: Ensures redirect URLs start with / for proper absolute redirects
  • Conditional Redirects: Only redirects when URL modification is needed based on the append setting

Redirect Logic:

  • When append is true (default):
    • URLs without trailing slashes get redirected to add one
    • URLs already ending with / are not modified
    • Root URL / is never modified
  • When append is false:
    • URLs with trailing slashes get redirected to remove them
    • Root URL / is never modified (exception case)
    • URLs without trailing slashes are not modified

Notes

  1. Middleware Order: This middleware must be placed after static file serving middleware (like connect.static() or express.static()) to avoid redirecting static file requests incorrectly
  2. Method Restriction: Only GET, HEAD, and OPTIONS requests are processed to prevent losing POST/PUT request data during redirects
  3. SEO Benefits: Helps create canonical URLs for better search engine optimization
  4. Performance: Minimal overhead as it only processes when redirects are actually needed