koa-sendfile

3.0.0 • Public • Published

koa sendfile

NPM version Build status Test coverage Dependency Status License Downloads

Basic file-sending utility for koa. It does the following:

  • Check if a file exists
  • Set content-length, content-type, and last-modified headers
  • 304 based on last-modified
  • Handle HEAD requests

It does not:

  • Check for malicious paths or hidden files
  • Support directory indexes
  • Cache control
  • OPTIONS method

API

sendfile(context, filename)

You must pass the koa context. filename is the filename of the file.

sendfile returns a promise that resolves to the fs.stat() result of the filename. If sendfile() resolves, that doesn't mean that a response is set - the filename could be a directory. Instead, check if (context.status).

const sendfile = require('koa-sendfile')
 
app.use(async function (ctx, next) {
  const stats = await sendfile(ctx, '/Users/jong/.bash_profile')
  if (!ctx.status) ctx.throw(404)
})

Readme

Keywords

none

Package Sidebar

Install

npm i koa-sendfile

Weekly Downloads

1,088

Version

3.0.0

License

MIT

Unpacked Size

5.7 kB

Total Files

5

Last publish

Collaborators

  • coderhaoxin
  • niftylettuce
  • aaron
  • juliangruber
  • eivifj
  • dead_horse
  • tjholowaychuk
  • bitinn
  • jongleberry
  • fengmk2
  • titanism