You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

182 lines
5.2 KiB

8 years ago
  1. # send
  2. [![NPM Version][npm-image]][npm-url]
  3. [![NPM Downloads][downloads-image]][downloads-url]
  4. [![Build Status][travis-image]][travis-url]
  5. [![Test Coverage][coveralls-image]][coveralls-url]
  6. [![Gittip][gittip-image]][gittip-url]
  7. Send is Connect's `static()` extracted for generalized use, a streaming static file
  8. server supporting partial responses (Ranges), conditional-GET negotiation, high test coverage, and granular events which may be leveraged to take appropriate actions in your application or framework.
  9. ## Installation
  10. ```bash
  11. $ npm install send
  12. ```
  13. ## API
  14. ```js
  15. var send = require('send')
  16. ```
  17. ### send(req, path, [options])
  18. Create a new `SendStream` for the given path to send to a `res`. The `req` is
  19. the Node.js HTTP request and the `path` is a urlencoded path to send (urlencoded,
  20. not the actual file-system path).
  21. #### Options
  22. ##### dotfiles
  23. Set how "dotfiles" are treated when encountered. A dotfile is a file
  24. or directory that begins with a dot ("."). Note this check is done on
  25. the path itself without checking if the path actually exists on the
  26. disk. If `root` is specified, only the dotfiles above the root are
  27. checked (i.e. the root itself can be within a dotfile when when set
  28. to "deny").
  29. The default value is `'ignore'`.
  30. - `'allow'` No special treatment for dotfiles.
  31. - `'deny'` Send a 403 for any request for a dotfile.
  32. - `'ignore'` Pretend like the dotfile does not exist and 404.
  33. ##### etag
  34. Enable or disable etag generation, defaults to true.
  35. ##### extensions
  36. If a given file doesn't exist, try appending one of the given extensions,
  37. in the given order. By default, this is disabled (set to `false`). An
  38. example value that will serve extension-less HTML files: `['html', 'htm']`.
  39. This is skipped if the requested file already has an extension.
  40. ##### index
  41. By default send supports "index.html" files, to disable this
  42. set `false` or to supply a new index pass a string or an array
  43. in preferred order.
  44. ##### lastModified
  45. Enable or disable `Last-Modified` header, defaults to true. Uses the file
  46. system's last modified value.
  47. ##### maxAge
  48. Provide a max-age in milliseconds for http caching, defaults to 0.
  49. This can also be a string accepted by the
  50. [ms](https://www.npmjs.org/package/ms#readme) module.
  51. ##### root
  52. Serve files relative to `path`.
  53. ### Events
  54. The `SendStream` is an event emitter and will emit the following events:
  55. - `error` an error occurred `(err)`
  56. - `directory` a directory was requested
  57. - `file` a file was requested `(path, stat)`
  58. - `headers` the headers are about to be set on a file `(res, path, stat)`
  59. - `stream` file streaming has started `(stream)`
  60. - `end` streaming has completed
  61. ### .pipe
  62. The `pipe` method is used to pipe the response into the Node.js HTTP response
  63. object, typically `send(req, path, options).pipe(res)`.
  64. ## Error-handling
  65. By default when no `error` listeners are present an automatic response will be made, otherwise you have full control over the response, aka you may show a 5xx page etc.
  66. ## Caching
  67. It does _not_ perform internal caching, you should use a reverse proxy cache such
  68. as Varnish for this, or those fancy things called CDNs. If your application is small enough that it would benefit from single-node memory caching, it's small enough that it does not need caching at all ;).
  69. ## Debugging
  70. To enable `debug()` instrumentation output export __DEBUG__:
  71. ```
  72. $ DEBUG=send node app
  73. ```
  74. ## Running tests
  75. ```
  76. $ npm install
  77. $ npm test
  78. ```
  79. ## Examples
  80. Small:
  81. ```js
  82. var http = require('http');
  83. var send = require('send');
  84. var app = http.createServer(function(req, res){
  85. send(req, req.url).pipe(res);
  86. }).listen(3000);
  87. ```
  88. Serving from a root directory with custom error-handling:
  89. ```js
  90. var http = require('http');
  91. var send = require('send');
  92. var url = require('url');
  93. var app = http.createServer(function(req, res){
  94. // your custom error-handling logic:
  95. function error(err) {
  96. res.statusCode = err.status || 500;
  97. res.end(err.message);
  98. }
  99. // your custom headers
  100. function headers(res, path, stat) {
  101. // serve all files for download
  102. res.setHeader('Content-Disposition', 'attachment');
  103. }
  104. // your custom directory handling logic:
  105. function redirect() {
  106. res.statusCode = 301;
  107. res.setHeader('Location', req.url + '/');
  108. res.end('Redirecting to ' + req.url + '/');
  109. }
  110. // transfer arbitrary files from within
  111. // /www/example.com/public/*
  112. send(req, url.parse(req.url).pathname, {root: '/www/example.com/public'})
  113. .on('error', error)
  114. .on('directory', redirect)
  115. .on('headers', headers)
  116. .pipe(res);
  117. }).listen(3000);
  118. ```
  119. ## License
  120. [MIT](LICENSE)
  121. [npm-image]: https://img.shields.io/npm/v/send.svg?style=flat
  122. [npm-url]: https://npmjs.org/package/send
  123. [travis-image]: https://img.shields.io/travis/tj/send.svg?style=flat
  124. [travis-url]: https://travis-ci.org/tj/send
  125. [coveralls-image]: https://img.shields.io/coveralls/tj/send.svg?style=flat
  126. [coveralls-url]: https://coveralls.io/r/tj/send?branch=master
  127. [downloads-image]: https://img.shields.io/npm/dm/send.svg?style=flat
  128. [downloads-url]: https://npmjs.org/package/send
  129. [gittip-image]: https://img.shields.io/gittip/dougwilson.svg?style=flat
  130. [gittip-url]: https://www.gittip.com/dougwilson/