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 Enable or disable etag generation, defaults to true.
60 If a given file doesn't exist, try appending one of the given extensions,
61 in the given order. By default, this is disabled (set to `false`). An
62 example value that will serve extension-less HTML files: `['html', 'htm']`.
63 This is skipped if the requested file already has an extension.
67 By default send supports "index.html" files, to disable this
68 set `false` or to supply a new index pass a string or an array
73 Enable or disable `Last-Modified` header, defaults to true. Uses the file
74 system's last modified value.
78 Provide a max-age in milliseconds for http caching, defaults to 0.
79 This can also be a string accepted by the
80 [ms](https://www.npmjs.org/package/ms#readme) module.
84 Serve files relative to `path`.
88 The `SendStream` is an event emitter and will emit the following events:
90 - `error` an error occurred `(err)`
91 - `directory` a directory was requested
92 - `file` a file was requested `(path, stat)`
93 - `headers` the headers are about to be set on a file `(res, path, stat)`
94 - `stream` file streaming has started `(stream)`
95 - `end` streaming has completed
99 The `pipe` method is used to pipe the response into the Node.js HTTP response
100 object, typically `send(req, path, options).pipe(res)`.
104 By default when no `error` listeners are present an automatic response will be
105 made, otherwise you have full control over the response, aka you may show a 5xx
110 It does _not_ perform internal caching, you should use a reverse proxy cache
111 such as Varnish for this, or those fancy things called CDNs. If your
112 application is small enough that it would benefit from single-node memory
113 caching, it's small enough that it does not need caching at all ;).
117 To enable `debug()` instrumentation output export __DEBUG__:
120 $ DEBUG=send node app
135 var http = require('http');
136 var send = require('send');
138 var app = http.createServer(function(req, res){
139 send(req, req.url).pipe(res);
143 Serving from a root directory with custom error-handling:
146 var http = require('http');
147 var send = require('send');
148 var url = require('url');
150 var app = http.createServer(function(req, res){
151 // your custom error-handling logic:
152 function error(err) {
153 res.statusCode = err.status || 500;
154 res.end(err.message);
157 // your custom headers
158 function headers(res, path, stat) {
159 // serve all files for download
160 res.setHeader('Content-Disposition', 'attachment');
163 // your custom directory handling logic:
164 function redirect() {
165 res.statusCode = 301;
166 res.setHeader('Location', req.url + '/');
167 res.end('Redirecting to ' + req.url + '/');
170 // transfer arbitrary files from within
171 // /www/example.com/public/*
172 send(req, url.parse(req.url).pathname, {root: '/www/example.com/public'})
174 .on('directory', redirect)
175 .on('headers', headers)
184 [npm-image]: https://img.shields.io/npm/v/send.svg
185 [npm-url]: https://npmjs.org/package/send
186 [travis-image]: https://img.shields.io/travis/pillarjs/send/master.svg?label=linux
187 [travis-url]: https://travis-ci.org/pillarjs/send
188 [appveyor-image]: https://img.shields.io/appveyor/ci/dougwilson/send/master.svg?label=windows
189 [appveyor-url]: https://ci.appveyor.com/project/dougwilson/send
190 [coveralls-image]: https://img.shields.io/coveralls/pillarjs/send/master.svg
191 [coveralls-url]: https://coveralls.io/r/pillarjs/send?branch=master
192 [downloads-image]: https://img.shields.io/npm/dm/send.svg
193 [downloads-url]: https://npmjs.org/package/send
194 [gratipay-image]: https://img.shields.io/gratipay/dougwilson.svg
195 [gratipay-url]: https://www.gratipay.com/dougwilson/