Express Slash Middleware
raw JSON →express-slash is an Express.js middleware designed for web applications that enforce strict routing rules regarding trailing slashes in URLs. It automatically handles `GET` and `HEAD` requests, redirecting clients (with a 301 status code by default) to the canonical URL with or without a trailing slash, based on the application's configured routes and `strict routing` setting. This package ensures URL consistency and prevents 404 errors for users who might incorrectly add or omit trailing slashes. The current stable version is 2.0.1, specifically compatible with Express 4.x. Version 1.x was for Express 3.x. The package has not seen active development in many years, suggesting it is in an abandoned state. While alternatives exist, its direct integration with Express's `strict routing` is a key differentiator for maintaining consistent URL structures programmatically.
Common errors
error TypeError: app.use() requires middleware functions but got a 'Object' ↓
express-slash as a function: app.use(slash()); error Error: Cannot GET /path (and no redirection occurs) ↓
app.enable('strict routing'); is called, and app.use(slash()); comes *after* app.use(router);. Also, ensure strict is set to true when creating express.Router() instances if not inheriting from app.get('strict routing'). error Maximum call stack size exceeded / too many redirects ↓
strict routing settings are consistent across app and all express.Router() instances. Ensure express-slash is placed correctly after routers and check for any routes that might conflict with its redirection logic, especially parameterized routes with optional trailing slashes. Warnings
breaking Breaking changes exist between major versions. `express-slash` v1.x is for Express 3.x, while v2.x is for Express 4.x. Using an incorrect version will lead to compatibility issues or runtime errors. ↓
gotcha The `express-slash` middleware *must* be added after your application's `router` middleware. Placing it before the router will prevent it from correctly identifying and redirecting unmatched routes based on trailing slashes. ↓
gotcha This middleware requires `app.enable('strict routing')` to be set in your Express application. Without strict routing, Express treats `/path` and `/path/` as the same, making `express-slash` ineffective for its intended purpose. ↓
gotcha When using multiple routers or mounted paths, ensure that `strict routing` is also enabled on each `express.Router()` instance to prevent unexpected behavior or redirection loops. If a router is mounted with a trailing slash prefix (e.g., `app.use('/series/', seriesRouter)`), but the `seriesRouter` itself doesn't have strict routing enabled or has conflicting slash logic, it can lead to infinite redirects. ↓
deprecated The `express-slash` package has not been updated in many years (last publish 12 years ago for version 2.0.1). While it works with Express 4.x, it is not maintained and is unlikely to be compatible with Express 5.x or future versions. Express 5.x itself has improved default handling for strict routing. ↓
Install
npm install express-slash yarn add express-slash pnpm add express-slash Imports
- slash wrong
import slash from 'express-slash';correctconst slash = require('express-slash'); - express wrong
import express from 'express';correctconst express = require('express');
Quickstart
const express = require('express');
const slash = require('express-slash');
const app = express();
// Important: Enable strict routing in Express for this middleware to be effective.
app.enable('strict routing');
// Create the router using the same routing options as the app.
const router = express.Router({
caseSensitive: app.get('case sensitive routing'),
strict : app.get('strict routing')
});
// Define some routes. Note the trailing slash on '/about/'.
router.get('/', function (req, res) {
res.send('Home Page');
});
router.get('/about/', function (req, res) {
res.send('About Us');
});
router.get('/contact', function (req, res) {
res.send('Contact Page');
});
// Add the `router` middleware first.
app.use(router);
// Add the `slash()` middleware *after* your app's `router`.
// Optionally specify an HTTP status code for redirection (defaults to 301).
app.use(slash());
const port = 3000;
app.listen(port, () => {
console.log(`Server listening on http://localhost:${port}`);
console.log(`Try http://localhost:${port}/about (will redirect to /about/)`);
console.log(`Try http://localhost:${port}/contact/ (will redirect to /contact)`);
});