3 [![NPM Version][npm-image]][npm-url]
4 [![NPM Downloads][downloads-image]][downloads-url]
5 [![Linux Build][travis-image]][travis-url]
6 [![Windows Build][appveyor-image]][appveyor-url]
7 [![Test Coverage][coveralls-image]][coveralls-url]
8 [![Gratipay][gratipay-image]][gratipay-url]
10 Send is a library for streaming files from the file system as a http response
11 supporting partial responses (Ranges), conditional-GET negotiation, high test
12 coverage, and granular events which may be leveraged to take appropriate actions
13 in your application or framework.
15 Looking to serve up entire folders mapped to URLs? Try [serve-static](https://www.npmjs.org/package/serve-static).
26 var send = require('send')
29 ### send(req, path, [options])
31 Create a new `SendStream` for the given path to send to a `res`. The `req` is
32 the Node.js HTTP request and the `path` is a urlencoded path to send (urlencoded,
33 not the actual file-system path).
39 Set how "dotfiles" are treated when encountered. A dotfile is a file
40 or directory that begins with a dot ("."). Note this check is done on
41 the path itself without checking if the path actually exists on the
42 disk. If `root` is specified, only the dotfiles above the root are
43 checked (i.e. the root itself can be within a dotfile when when set
46 - `'allow'` No special treatment for dotfiles.
47 - `'deny'` Send a 403 for any request for a dotfile.
48 - `'ignore'` Pretend like the dotfile does not exist and 404.
50 The default value is _similar_ to `'ignore'`, with the exception that
51 this default will not ignore the files within a directory that begins
52 with a dot, for backward-compatibility.
56 Byte offset at which the stream ends, defaults to the length of the file
57 minus 1. The end is inclusive in the stream, meaning `end: 3` will include
58 the 4th byte in the stream.
62 Enable or disable etag generation, defaults to true.
66 If a given file doesn't exist, try appending one of the given extensions,
67 in the given order. By default, this is disabled (set to `false`). An
68 example value that will serve extension-less HTML files: `['html', 'htm']`.
69 This is skipped if the requested file already has an extension.
73 By default send supports "index.html" files, to disable this
74 set `false` or to supply a new index pass a string or an array
79 Enable or disable `Last-Modified` header, defaults to true. Uses the file
80 system's last modified value.
84 Provide a max-age in milliseconds for http caching, defaults to 0.
85 This can also be a string accepted by the
86 [ms](https://www.npmjs.org/package/ms#readme) module.
90 Serve files relative to `path`.
94 Byte offset at which the stream starts, defaults to 0. The start is inclusive,
95 meaning `start: 2` will include the 3rd byte in the stream.
99 The `SendStream` is an event emitter and will emit the following events:
101 - `error` an error occurred `(err)`
102 - `directory` a directory was requested
103 - `file` a file was requested `(path, stat)`
104 - `headers` the headers are about to be set on a file `(res, path, stat)`
105 - `stream` file streaming has started `(stream)`
106 - `end` streaming has completed
110 The `pipe` method is used to pipe the response into the Node.js HTTP response
111 object, typically `send(req, path, options).pipe(res)`.
115 The `mime` export is the global instance of of the
116 [`mime` npm module](https://www.npmjs.com/package/mime).
118 This is used to configure the MIME types that are associated with file extensions
119 as well as other options for how to resolve the MIME type of a file (like the
120 default type to use for an unknown file extension).
124 By default when no `error` listeners are present an automatic response will be
125 made, otherwise you have full control over the response, aka you may show a 5xx
130 It does _not_ perform internal caching, you should use a reverse proxy cache
131 such as Varnish for this, or those fancy things called CDNs. If your
132 application is small enough that it would benefit from single-node memory
133 caching, it's small enough that it does not need caching at all ;).
137 To enable `debug()` instrumentation output export __DEBUG__:
140 $ DEBUG=send node app
155 var http = require('http');
156 var send = require('send');
158 var app = http.createServer(function(req, res){
159 send(req, req.url).pipe(res);
163 ### Custom file types
166 var http = require('http');
167 var send = require('send');
169 // Default unknown types to text/plain
170 send.mime.default_type = 'text/plain';
174 'application/x-my-type': ['x-mt', 'x-mtt']
177 var app = http.createServer(function(req, res){
178 send(req, req.url).pipe(res);
182 ### Serving from a root directory with custom error-handling
185 var http = require('http');
186 var send = require('send');
187 var url = require('url');
189 var app = http.createServer(function(req, res){
190 // your custom error-handling logic:
191 function error(err) {
192 res.statusCode = err.status || 500;
193 res.end(err.message);
196 // your custom headers
197 function headers(res, path, stat) {
198 // serve all files for download
199 res.setHeader('Content-Disposition', 'attachment');
202 // your custom directory handling logic:
203 function redirect() {
204 res.statusCode = 301;
205 res.setHeader('Location', req.url + '/');
206 res.end('Redirecting to ' + req.url + '/');
209 // transfer arbitrary files from within
210 // /www/example.com/public/*
211 send(req, url.parse(req.url).pathname, {root: '/www/example.com/public'})
213 .on('directory', redirect)
214 .on('headers', headers)
223 [npm-image]: https://img.shields.io/npm/v/send.svg
224 [npm-url]: https://npmjs.org/package/send
225 [travis-image]: https://img.shields.io/travis/pillarjs/send/master.svg?label=linux
226 [travis-url]: https://travis-ci.org/pillarjs/send
227 [appveyor-image]: https://img.shields.io/appveyor/ci/dougwilson/send/master.svg?label=windows
228 [appveyor-url]: https://ci.appveyor.com/project/dougwilson/send
229 [coveralls-image]: https://img.shields.io/coveralls/pillarjs/send/master.svg
230 [coveralls-url]: https://coveralls.io/r/pillarjs/send?branch=master
231 [downloads-image]: https://img.shields.io/npm/dm/send.svg
232 [downloads-url]: https://npmjs.org/package/send
233 [gratipay-image]: https://img.shields.io/gratipay/dougwilson.svg
234 [gratipay-url]: https://www.gratipay.com/dougwilson/