{"id":18247,"library":"connect-slashes","title":"connect-slashes","description":"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.","status":"active","version":"1.4.0","language":"javascript","source_language":"en","source_url":"https://github.com/avinoamr/connect-slashes","tags":["javascript","trailing","slash","connect","middleware","express"],"install":[{"cmd":"npm install connect-slashes","lang":"bash","label":"npm"},{"cmd":"yarn add connect-slashes","lang":"bash","label":"yarn"},{"cmd":"pnpm add connect-slashes","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"ESM and CommonJS both supported by the package (no exports field). CommonJS require works, but named imports are not available.","wrong":"const slashes = require('connect-slashes')","symbol":"default","correct":"import slashes from 'connect-slashes'"},{"note":"CommonJS is the original pattern; named import is incorrect.","wrong":"import { slashes } from 'connect-slashes'","symbol":"default (CommonJS)","correct":"const slashes = require('connect-slashes')"},{"note":"No TypeScript types included; use @types/connect-slashes if needed.","wrong":"import * as slashes from 'connect-slashes'","symbol":"TypeScript usage","correct":"import slashes from 'connect-slashes'"}],"quickstart":{"code":"const express = require('express');\nconst slashes = require('connect-slashes');\n\nconst app = express();\n// Place after static middleware\napp.use(express.static('public'));\napp.use(slashes(true, { base: '/blog', code: 301, headers: { 'Cache-Control': 'public' } }));\napp.get('/blog/post', (req, res) => res.send('Post'));\napp.listen(3000);","lang":"javascript","description":"Sets up an Express server that appends trailing slashes to URLs under /blog, using 301 redirects with custom headers."},"warnings":[{"fix":"Order middleware correctly: static first, then slashes.","message":"Middleware must be placed after connect.static() to avoid breaking static file URLs (e.g., /app.css becomes /app.css/).","severity":"gotcha","affected_versions":"*"},{"fix":"This is intentional; no fix needed.","message":"Only GET, HEAD, and OPTIONS requests are redirected. POST/PUT requests are unaffected.","severity":"gotcha","affected_versions":"*"},{"fix":"Pass { code: 302 } as second argument to slashes() for temporary redirects.","message":"Redirects default to 301 (permanent). Check if temporary redirects (302) are more appropriate for your use case.","severity":"gotcha","affected_versions":"*"},{"fix":"Set the `base` option correctly, e.g., { base: '/blog' }.","message":"The `base` option prepends a base URL to the redirect path. Ensure it matches your reverse proxy configuration.","severity":"gotcha","affected_versions":"*"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Run `npm install connect-slashes` in your project directory.","cause":"Package not installed or not in node_modules.","error":"Cannot find module 'connect-slashes'"},{"fix":"Use `import slashes from 'connect-slashes'` or `const slashes = require('connect-slashes')`.","cause":"Importing incorrectly, e.g., using named import when default export is used.","error":"TypeError: slashes is not a function"},{"fix":"Move slashes middleware after static middleware in the middleware stack.","cause":"Middleware applied before static middleware, causing trailing slash to be appended to file URLs.","error":"404 on static files after adding slashes middleware"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}