Registry / web-framework / connect-slashes

connect-slashes

JSON →
library1.4.0jsnpmunverified

Trailing slash redirect middleware for Connect and Express.js. Version 1.4.0 is the current stable release, with no predictable cadence. It automatically appends or removes trailing slashes from URLs to enforce canonical URLs. Unlike alternatives like `express-slash`, it uses 301 redirects by default and supports custom base paths and headers. It only affects GET, HEAD, and OPTIONS requests to avoid data loss on POST/PUT.

npm install connect-slashes
INSTALL
IMPORT
SIG · CONNECT-SLASHES
C
connect-slashes
web-frameworkjavascriptv1.4.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
import slashes from 'connect-slashes'
const slashes = require('connect-slashes')
ESM and CommonJS both supported by the package (no exports field). CommonJS require works, but named imports are not available.
default (CommonJS)
const slashes = require('connect-slashes')
import { slashes } from 'connect-slashes'
CommonJS is the original pattern; named import is incorrect.
TypeScript usage
import slashes from 'connect-slashes'
import * as slashes from 'connect-slashes'
No TypeScript types included; use @types/connect-slashes if needed.

Sets up an Express server that appends trailing slashes to URLs under /blog, using 301 redirects with custom headers.

const express = require('express'); const slashes = require('connect-slashes'); const app = express(); // Place after static middleware app.use(express.static('public')); app.use(slashes(true, { base: '/blog', code: 301, headers: { 'Cache-Control': 'public' } })); app.get('/blog/post', (req, res) => res.send('Post')); app.listen(3000);
Debug
Known issues
gotchaMiddleware must be placed after connect.static() to avoid breaking static file URLs (e.g., /app.css becomes /app.css/).
fix
Order middleware correctly: static first, then slashes.
affects: *
gotchaOnly GET, HEAD, and OPTIONS requests are redirected. POST/PUT requests are unaffected.
fix
This is intentional; no fix needed.
affects: *
gotchaRedirects default to 301 (permanent). Check if temporary redirects (302) are more appropriate for your use case.
fix
Pass { code: 302 } as second argument to slashes() for temporary redirects.
affects: *
gotchaThe `base` option prepends a base URL to the redirect path. Ensure it matches your reverse proxy configuration.
fix
Set the `base` option correctly, e.g., { base: '/blog' }.
affects: *
Errors
Common errors & fixes
Cannot find module 'connect-slashes'
Package not installed or not in node_modules.
fix
Run `npm install connect-slashes` in your project directory.
TypeError: slashes is not a function
Importing incorrectly, e.g., using named import when default export is used.
fix
Use `import slashes from 'connect-slashes'` or `const slashes = require('connect-slashes')`.
404 on static files after adding slashes middleware
Middleware applied before static middleware, causing trailing slash to be appended to file URLs.
fix
Move slashes middleware after static middleware in the middleware stack.
Upgrade
Version history
1.4.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
5 hits · last 30 days
node
4
OpenAI (training)
1
Resources
connect-slashes — npm install connect-slashes · libregistry