(req, res, next)
| 1 | import { defaultCacheControl } from './cache-control.js' |
| 2 | |
| 3 | export default function trailingSlashes(req, res, next) { |
| 4 | if (req.method === 'GET' || req.method === 'HEAD' || req.method === 'OPTIONS') { |
| 5 | const split = req.url.split('?') |
| 6 | let pathname = split.shift() |
| 7 | if (pathname !== '/' && pathname.endsWith('/')) { |
| 8 | while (pathname.endsWith('/')) { |
| 9 | pathname = pathname.slice(0, pathname.length - 1) |
| 10 | } |
| 11 | let url = pathname |
| 12 | if (split.length) { |
| 13 | url += `?${split.join('?')}` |
| 14 | } |
| 15 | defaultCacheControl(res) |
| 16 | return res.redirect(301, url) |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | next() |
| 21 | } |
nothing calls this directly
no test coverage detected