{"id":17638,"library":"express-slash","title":"Express Slash Middleware","description":"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.","status":"abandoned","version":"2.0.1","language":"javascript","source_language":"en","source_url":"git://github.com/ericf/express-slash","tags":["javascript","express","express3","url","urls","slash","trailing","slashes","route"],"install":[{"cmd":"npm install express-slash","lang":"bash","label":"npm"},{"cmd":"yarn add express-slash","lang":"bash","label":"yarn"},{"cmd":"pnpm add express-slash","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Runtime dependency as a middleware for Express.js. Specific versions of express-slash are tied to major Express versions (v2.x for Express 4.x).","package":"express","optional":false}],"imports":[{"note":"This package is CommonJS-only. Attempting to use ESM `import` syntax will result in an error.","wrong":"import slash from 'express-slash';","symbol":"slash","correct":"const slash = require('express-slash');"},{"note":"Express.js itself, which this middleware depends on, is commonly imported via `require` in projects compatible with `express-slash` v2.x.","wrong":"import express from 'express';","symbol":"express","correct":"const express = require('express');"}],"quickstart":{"code":"const express = require('express');\nconst slash = require('express-slash');\n\nconst app = express();\n\n// Important: Enable strict routing in Express for this middleware to be effective.\napp.enable('strict routing');\n\n// Create the router using the same routing options as the app.\nconst router = express.Router({\n    caseSensitive: app.get('case sensitive routing'),\n    strict       : app.get('strict routing')\n});\n\n// Define some routes. Note the trailing slash on '/about/'.\nrouter.get('/', function (req, res) {\n    res.send('Home Page');\n});\n\nrouter.get('/about/', function (req, res) {\n    res.send('About Us');\n});\n\nrouter.get('/contact', function (req, res) {\n    res.send('Contact Page');\n});\n\n// Add the `router` middleware first.\napp.use(router);\n\n// Add the `slash()` middleware *after* your app's `router`.\n// Optionally specify an HTTP status code for redirection (defaults to 301).\napp.use(slash());\n\nconst port = 3000;\napp.listen(port, () => {\n    console.log(`Server listening on http://localhost:${port}`);\n    console.log(`Try http://localhost:${port}/about (will redirect to /about/)`);\n    console.log(`Try http://localhost:${port}/contact/ (will redirect to /contact)`);\n});","lang":"javascript","description":"This quickstart demonstrates how to set up `express-slash` with an Express application, enabling strict routing and ensuring proper trailing slash redirection for defined routes. It highlights the correct order of middleware."},"warnings":[{"fix":"Ensure `express-slash` v2.x is used for Express 4.x applications. For Express 5.x, this package is not officially supported and may not function as expected due to changes in Express's routing. Consider alternative solutions for newer Express versions.","message":"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.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Ensure your `app.use(slash())` call appears *after* `app.use(router)` or any other route-defining middleware.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Add `app.enable('strict routing');` early in your Express application setup.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"When creating a router, pass the `strict` option: `const router = express.Router({ strict: app.get('strict routing') });`","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"For new projects or migration to Express 5.x, consider leveraging Express's built-in `strict routing` options directly or exploring more actively maintained middleware alternatives for trailing slash management. Evaluate if `express-slash` is still necessary given Express 5.x's enhanced defaults and native support for features like async/await in middleware.","message":"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.","severity":"deprecated","affected_versions":">=2.0.0"}],"env_vars":null,"last_verified":"2026-04-23T00:00:00.000Z","next_check":"2026-07-22T00:00:00.000Z","problems":[{"fix":"Always call `express-slash` as a function: `app.use(slash());`","cause":"Attempting to use `express-slash` without calling it as a function, e.g., `app.use(slash);` instead of `app.use(slash());`","error":"TypeError: app.use() requires middleware functions but got a 'Object'"},{"fix":"Ensure `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')`.","cause":"The `express-slash` middleware is placed *before* the main Express router, or `app.enable('strict routing')` is not set, or the router itself doesn't have strict routing enabled.","error":"Error: Cannot GET /path (and no redirection occurs)"},{"fix":"Verify that `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.","cause":"A redirection loop is occurring, often due to conflicting strict routing settings between the main app and a sub-router, or if `express-slash` is misconfigured with a wildcard route (e.g., `router.get('/:param/'`) where the middleware keeps toggling the slash.","error":"Maximum call stack size exceeded / too many redirects"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}