|
1 | | -# [**koa-send**](https://github.com/koajs/send) |
| 1 | +# [**@koa/send**](https://github.com/koajs/send) |
2 | 2 |
|
3 | | -> Static file serving middleware. |
4 | 3 |
|
5 | 4 | [![NPM version][npm-image]][npm-url] |
6 | | -[![Build status][travis-image]][travis-url] |
| 5 | +![Build Status][github-action-image] |
7 | 6 | [![Test coverage][coveralls-image]][coveralls-url] |
8 | | -[![Dependency Status][david-image]][david-url] |
9 | 7 | [![License][license-image]][license-url] |
| 8 | +[![Node Version][node-image]][node-url] |
10 | 9 | [![Downloads][downloads-image]][downloads-url] |
11 | 10 |
|
| 11 | +[npm-image]: https://img.shields.io/npm/v/@koa/send.svg?style=flat-square |
| 12 | +[npm-url]: https://npmjs.org/package/@koa/send |
| 13 | +[github-action-image]: https://github.com/koajs/send/actions/workflows/ci.yml/badge.svg?style=flat-square |
| 14 | +[coveralls-image]: https://img.shields.io/coveralls/koajs/send.svg?style=flat-square |
| 15 | +[coveralls-url]: https://coveralls.io/r/koajs/send?branch=master |
| 16 | +[license-image]: http://img.shields.io/npm/l/koa-send.svg?style=flat-square |
| 17 | +[license-url]: LICENSE |
| 18 | +[downloads-image]: http://img.shields.io/npm/dm/koa-send.svg?style=flat-square |
| 19 | +[downloads-url]: https://npmjs.org/package/koa-send |
| 20 | + |
| 21 | +Koa static file serving middleware. |
| 22 | + |
| 23 | +## Install |
12 | 24 |
|
13 | | -## Installation |
| 25 | +[](https://nodei.co/npm/@koa/send) |
14 | 26 |
|
15 | 27 | ```js |
16 | | -$ npm install koa-send |
| 28 | +$ npm i @koa/send |
17 | 29 | ``` |
18 | 30 |
|
19 | | - |
20 | 31 | ## Options |
21 | 32 |
|
22 | | - - `maxage` Browser cache max-age in milliseconds. (defaults to `0`). |
23 | | - - `immutable` Tell the browser the resource is immutable and can be cached indefinitely. (defaults to `false`). |
24 | | - - `hidden` Allow transfer of hidden files. (defaults to `false`). |
25 | | - - [`root`](#root-path) Root directory to restrict file access. |
26 | | - - `index` Name of the index file to serve automatically when visiting the root location. (defaults to none). |
27 | | - - `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`). |
28 | | - - `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`). |
29 | | - - `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/`. |
30 | | - - [`setHeaders`](#setheaders) Function to set custom headers on response. |
31 | | - - `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`) |
| 33 | +- `maxage` Browser cache max-age in milliseconds. (defaults to `0`). |
| 34 | +- `immutable` Tell the browser the resource is immutable and can be cached indefinitely. (defaults to `false`). |
| 35 | +- `hidden` Allow transfer of hidden files. (defaults to `false`). |
| 36 | +- [`root`](#root-path) Root directory to restrict file access. |
| 37 | +- `index` Name of the index file to serve automatically when visiting the root location. (defaults to none). |
| 38 | +- `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`). |
| 39 | +- `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`). |
| 40 | +- `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/`. |
| 41 | +- [`setHeaders`](#setheaders) Function to set custom headers on response. |
| 42 | +- `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`) |
32 | 43 |
|
33 | 44 | ### Root path |
34 | 45 |
|
35 | | - Note that `root` is required, defaults to `''` and will be resolved, |
36 | | - removing the leading `/` to make the path relative and this |
37 | | - path must not contain "..", protecting developers from |
38 | | - concatenating user input. If you plan on serving files based on |
39 | | - user input supply a `root` directory from which to serve from. |
| 46 | +Note that `root` is required, defaults to `''` and will be resolved, |
| 47 | +removing the leading `/` to make the path relative and this |
| 48 | +path must not contain "..", protecting developers from |
| 49 | +concatenating user input. If you plan on serving files based on |
| 50 | +user input supply a `root` directory from which to serve from. |
40 | 51 |
|
41 | | - For example to serve files from `./public`: |
| 52 | +For example to serve files from `./public`: |
42 | 53 |
|
43 | 54 | ```js |
44 | 55 | app.use(async (ctx) => { |
45 | | - await send(ctx, ctx.path, { root: __dirname + '/public' }); |
46 | | -}) |
| 56 | + await send(ctx, ctx.path, { root: __dirname + "/public" }); |
| 57 | +}); |
47 | 58 | ``` |
48 | 59 |
|
49 | | - To serve developer specified files: |
| 60 | +To serve developer specified files: |
50 | 61 |
|
51 | 62 | ```js |
52 | 63 | app.use(async (ctx) => { |
53 | | - await send(ctx, 'path/to/my.js'); |
54 | | -}) |
| 64 | + await send(ctx, "path/to/my.js"); |
| 65 | +}); |
55 | 66 | ``` |
56 | 67 |
|
57 | 68 | ### setHeaders |
58 | 69 |
|
59 | 70 | The function is called as `fn(res, path, stats)`, where the arguments are: |
60 | | -* `res`: the response object. |
61 | | -* `path`: the resolved file path that is being sent. |
62 | | -* `stats`: the stats object of the file that is being sent. |
| 71 | + |
| 72 | +- `res`: the response object. |
| 73 | +- `path`: the resolved file path that is being sent. |
| 74 | +- `stats`: the stats object of the file that is being sent. |
63 | 75 |
|
64 | 76 | You should only use the `setHeaders` option when you wish to edit the `Cache-Control` or `Last-Modified` headers, because doing it before is useless (it's overwritten by `send`), and doing it after is too late because the headers are already sent. |
65 | 77 |
|
66 | 78 | If you want to edit any other header, simply set them before calling `send`. |
67 | 79 |
|
68 | | - |
69 | 80 | ## Example |
70 | 81 |
|
71 | 82 | ```js |
72 | | -const send = require('koa-send'); |
73 | | -const Koa = require('koa'); |
| 83 | +const send = require("koa-send"); |
| 84 | +const Koa = require("koa"); |
74 | 85 | const app = new Koa(); |
75 | 86 |
|
76 | 87 | // $ GET /package.json |
77 | 88 | // $ GET / |
78 | 89 |
|
79 | 90 | app.use(async (ctx) => { |
80 | | - if ('/' == ctx.path) return ctx.body = 'Try GET /package.json'; |
| 91 | + if ("/" == ctx.path) return (ctx.body = "Try GET /package.json"); |
81 | 92 | await send(ctx, ctx.path); |
82 | | -}) |
| 93 | +}); |
83 | 94 |
|
84 | 95 | app.listen(3000); |
85 | | -console.log('listening on port 3000'); |
| 96 | +console.log("listening on port 3000"); |
86 | 97 | ``` |
87 | 98 |
|
88 | | - |
89 | 99 | ## License |
90 | 100 |
|
91 | 101 | [MIT](/LICENSE) |
92 | | - |
93 | | - |
94 | | -[npm-image]: https://img.shields.io/npm/v/koa-send.svg?style=flat-square |
95 | | -[npm-url]: https://npmjs.org/package/koa-send |
96 | | -[github-tag]: http://img.shields.io/github/tag/koajs/send.svg?style=flat-square |
97 | | -[github-url]: https://github.com/koajs/send/tags |
98 | | -[travis-image]: https://img.shields.io/travis/koajs/send.svg?style=flat-square |
99 | | -[travis-url]: https://travis-ci.org/koajs/send |
100 | | -[coveralls-image]: https://img.shields.io/coveralls/koajs/send.svg?style=flat-square |
101 | | -[coveralls-url]: https://coveralls.io/r/koajs/send?branch=master |
102 | | -[david-image]: http://img.shields.io/david/koajs/send.svg?style=flat-square |
103 | | -[david-url]: https://david-dm.org/koajs/send |
104 | | -[license-image]: http://img.shields.io/npm/l/koa-send.svg?style=flat-square |
105 | | -[license-url]: LICENSE |
106 | | -[downloads-image]: http://img.shields.io/npm/dm/koa-send.svg?style=flat-square |
107 | | -[downloads-url]: https://npmjs.org/package/koa-send |
108 | | -[gittip-image]: https://img.shields.io/gittip/jonathanong.svg?style=flat-square |
109 | | -[gittip-url]: https://www.gittip.com/jonathanong/ |
0 commit comments