Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: koajs/send
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 134a6f62464286e4c3109086fb73b6cc052daf78
Choose a base ref
...
head repository: koajs/send
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 2ce173293d0e8ff02ea42ad844ce797799a4834f
Choose a head ref
Loading
Showing with 727 additions and 490 deletions.
  1. +2 −0 .eslintrc
  2. +1 −0 .gitignore
  3. +1 −0 .npmrc
  4. +2 −3 .travis.yml
  5. +30 −0 History.md
  6. +15 −31 Readme.md
  7. +11 −8 example.js
  8. +116 −102 index.js
  9. +24 −13 package.json
  10. +3 −0 test/.eslintrc
  11. +1 −0 test/fixtures/gzip.json.br
  12. +4 −0 test/fixtures/some.path/index.json
  13. +517 −333 test/index.js
2 changes: 2 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
extends: standard
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
package-lock.json
node_modules
coverage
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
5 changes: 2 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
node_js:
- "4"
- "6"
- "7"
- 8
- 10
sudo: false
language: node_js
script: "npm run-script test-travis"
30 changes: 30 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,34 @@

5.0.0 / 2018-06-19
==================

* bump deps
* fix bug that set Content-Type not working. (#105)

4.1.2 / 2017-12-14
==================

* Fix issue with dots in path when using extensions array (#92)

4.1.1 / 2017-09-25
==================

* Fix brotli support, closes #83
* Fix tests

4.1.0 / 2017-04-26
==================

* Add support for Cache-Control: immutable with a new "immutable" option
* Added serving of brotli versions of files

4.0.0 / 2017-04-09
==================

* throw error if file not exists, closes #43
* remove co, use async function
* bump deps

3.3.0 / 2017-01-10
==================

46 changes: 15 additions & 31 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -19,10 +19,13 @@ $ npm install koa-send
## Options

- `maxage` Browser cache max-age in milliseconds. (defaults to `0`)
- `immutable` Tell the browser the resource is immutable and can be cached indefinitely. (defaults to `false`)
- `hidden` Allow transfer of hidden files. (defaults to `false`)
- [`root`](#root-path) Root directory to restrict file access
- `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.
- `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/`
- [`root`](#root-path) Root directory to restrict file access.
- `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/`.
- [`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`)

@@ -37,16 +40,16 @@ $ npm install koa-send
For example to serve files from `./public`:

```js
app.use(function *(){
yield send(this, this.path, { root: __dirname + '/public' });
app.use(async (ctx) => {
await send(ctx, ctx.path, { root: __dirname + '/public' });
})
```

To serve developer specified files:

```js
app.use(function *(){
yield send(this, 'path/to/my.js');
app.use(async (ctx) => {
await send(ctx, 'path/to/my.js');
})
```

@@ -61,17 +64,17 @@ You should only use the `setHeaders` option when you wish to edit the `Cache-Con

If you want to edit any other header, simply set them before calling `send`.

## Example (Koa@2) with async/await
## Example

```js
var send = require('koa-send');
var Koa = require('koa');
var app = new Koa();
const send = require('koa-send');
const Koa = require('koa');
const app = new Koa();

// $ GET /package.json
// $ GET /

app.use(async function (ctx, next){
app.use(async (ctx) => {
if ('/' == ctx.path) return ctx.body = 'Try GET /package.json';
await send(ctx, ctx.path);
})
@@ -80,25 +83,6 @@ app.listen(3000);
console.log('listening on port 3000');
```

## Example

```js
var send = require('koa-send');
var koa = require('koa');
var app = koa();

// $ GET /package.json
// $ GET /

app.use(function *(){
if ('/' == this.path) return this.body = 'Try GET /package.json';
yield send(this, this.path);
})

app.listen(3000);
console.log('listening on port 3000');
```

## License

MIT
19 changes: 11 additions & 8 deletions example.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@

var send = require('./');
var koa = require('koa');
var app = koa();
const send = require('./')
const Koa = require('koa')
const app = new Koa()

// $ GET /package.json
// $ GET /

app.use(function *(){
if ('/' == this.path) return this.body = 'Try GET /package.json';
yield send(this, this.path, { root: __dirname });
app.use(async (ctx) => {
if (ctx.path === '/') {
ctx.body = 'Try GET /package.json'
return
}
await send(ctx, ctx.path, { root: __dirname })
})

app.listen(3000);
console.log('listening on port 3000');
app.listen(3000)
console.log('listening on port 3000')
Loading