Skip to content

Commit

Permalink
Merge ec2fae0 into f93573d
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnyxwan committed Feb 28, 2020
2 parents f93573d + ec2fae0 commit df7135c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
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
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

0 comments on commit df7135c

Please sign in to comment.