Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend format option to support redirection of trailing slash for directories #136

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ $ npm install koa-send
- `index` Name of the index file to serve automatically when visiting the root location. (defaults to none).
- `gzip` Try to serve the gzipped version of a file automatically when `gzip` is supported by a client and if the requested file with `.gz` extension exists. (defaults to `true`).
- `brotli` Try to serve the brotli version of a file automatically when `brotli` is supported by a client and if the requested file with `.br` extension exists. (defaults to `true`).
- `format` If not `false` (defaults to `true`), format the path to serve static file servers and not require a trailing slash for directories, so that you can do both `/directory` and `/directory/`.
- `format` If not `false` (defaults to `'follow'`), format the path to serve static file servers and not require a trailing slash for directories, so that you can do both `/directory` and `/directory/` transparently. If it is `'redirect'`, issue a HTTP 302 redirection to the url with trailing slash.
- [`setHeaders`](#setheaders) Function to set custom headers on response.
- `extensions` Try to match extensions from passed array to search for file when no extension is sufficed in URL. First found is served. (defaults to `false`)

Expand Down
9 changes: 7 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ async function send (ctx, path, opts = {}) {
const maxage = opts.maxage || opts.maxAge || 0
const immutable = opts.immutable || false
const hidden = opts.hidden || false
const format = opts.format !== false
const format = (opts.format === false) ? false : (opts.format === 'redirect' ? 'redirect' : 'follow')
const extensions = Array.isArray(opts.extensions) ? opts.extensions : false
const brotli = opts.brotli !== false
const gzip = opts.gzip !== false
Expand Down Expand Up @@ -107,10 +107,15 @@ async function send (ctx, path, opts = {}) {
// Format the path to serve static file servers
// and not require a trailing slash for directories,
// so that you can do both `/directory` and `/directory/`
// for 'follow', transparently send the file
// for 'redirect', give HTTP 302 redirection
if (stats.isDirectory()) {
if (format && index) {
if (format === 'follow' && index) {
path += `/${index}`
stats = await fs.stat(path)
} else if (format === 'redirect' && index) {
ctx.redirect(ctx.request.path + '/' + ctx.request.search)
return true
} else {
return
}
Expand Down