Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/koajs/sendfile
basic file-sending utility for koa
https://github.com/koajs/sendfile
Last synced: 2 months ago
JSON representation
basic file-sending utility for koa
- Host: GitHub
- URL: https://github.com/koajs/sendfile
- Owner: koajs
- License: mit
- Created: 2014-03-23T11:22:26.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2023-01-16T13:08:02.000Z (almost 2 years ago)
- Last Synced: 2024-04-14T13:08:40.724Z (8 months ago)
- Language: JavaScript
- Homepage:
- Size: 27.3 KB
- Stars: 47
- Watchers: 7
- Forks: 8
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- Changelog: History.md
- License: LICENSE
Awesome Lists containing this project
- awesome-koa - sendfile - basic file-sending utility for koa (Middleware)
- awesome-koa - koa-sendfile - 基础文件发送工具。 ![](https://img.shields.io/github/stars/koajs/sendfile.svg?style=social&label=Star) ![](https://img.shields.io/npm/dm/koa-sendfile.svg?style=flat-square) (仓库 / 中间件)
README
# koa sendfile
[![NPM version][npm-image]][npm-url]
[![Build status][travis-image]][travis-url]
[![Test coverage][coveralls-image]][coveralls-url]
[![Dependency Status][david-image]][david-url]
[![License][license-image]][license-url]
[![Downloads][downloads-image]][downloads-url]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 requestsIt 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)`.
```js
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)
})
```[npm-image]: https://img.shields.io/npm/v/koa-sendfile.svg?style=flat-square
[npm-url]: https://npmjs.org/package/koa-sendfile
[github-tag]: http://img.shields.io/github/tag/koajs/sendfile.svg?style=flat-square
[github-url]: https://github.com/koajs/sendfile/tags
[travis-image]: https://img.shields.io/travis/koajs/sendfile.svg?style=flat-square
[travis-url]: https://travis-ci.org/koajs/sendfile
[coveralls-image]: https://img.shields.io/coveralls/koajs/sendfile.svg?style=flat-square
[coveralls-url]: https://coveralls.io/r/koajs/sendfile?branch=master
[david-image]: http://img.shields.io/david/koajs/sendfile.svg?style=flat-square
[david-url]: https://david-dm.org/koajs/sendfile
[license-image]: http://img.shields.io/npm/l/koa-sendfile.svg?style=flat-square
[license-url]: LICENSE
[downloads-image]: http://img.shields.io/npm/dm/koa-sendfile.svg?style=flat-square
[downloads-url]: https://npmjs.org/package/koa-sendfile