node package manager


basic file-sending utility for koa

koa sendfile

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

You must pass this as the context. filename is the filename of the file.

stats is the fs.stat() result of the filename. If stats exists, that doesn't mean that a response is set - the filename could be a directory. Instead, check if (this.status).

var sendfile = require('koa-sendfile')
app.use(function* (next) {
  var stats = yield*, '/Users/jong/.bash_profile')
  if (!this.status) this.throw(404)