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.

122 lines
3.4 KiB

  1. # send
  2. Send is Connect's `static()` extracted for generalized use, a streaming static file
  3. 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.
  4. ## Installation
  5. $ npm install send
  6. ## Examples
  7. Small:
  8. ```js
  9. var http = require('http');
  10. var send = require('send');
  11. var app = http.createServer(function(req, res){
  12. send(req, req.url).pipe(res);
  13. });
  14. ```
  15. Serving from a root directory with custom error-handling:
  16. ```js
  17. var http = require('http');
  18. var send = require('send');
  19. var app = http.createServer(function(req, res){
  20. // your custom error-handling logic:
  21. function error(err) {
  22. res.statusCode = err.status || 500;
  23. res.end(err.message);
  24. }
  25. // your custom directory handling logic:
  26. function redirect() {
  27. res.statusCode = 301;
  28. res.setHeader('Location', req.url + '/');
  29. res.end('Redirecting to ' + req.url + '/');
  30. }
  31. // transfer arbitrary files from within
  32. // /www/example.com/public/*
  33. send(req, url.parse(req.url).pathname)
  34. .root('/www/example.com/public')
  35. .on('error', error)
  36. .on('directory', redirect)
  37. .pipe(res);
  38. });
  39. ```
  40. ## API
  41. ### Events
  42. - `error` an error occurred `(err)`
  43. - `directory` a directory was requested
  44. - `stream` file streaming has started `(stream)`
  45. - `end` streaming has completed
  46. ### .root(dir)
  47. Serve files relative to `path`. Aliased as `.from(dir)`.
  48. ### .index(path)
  49. By default send supports "index.html" files, to disable this
  50. invoke `.index(false)` or to supply a new index pass a string.
  51. ### .maxage(ms)
  52. Provide a max-age in milliseconds for http caching, defaults to 0.
  53. ## Error-handling
  54. 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.
  55. ## Caching
  56. It does _not_ perform internal caching, you should use a reverse proxy cache such
  57. 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 ;).
  58. ## Debugging
  59. To enable `debug()` instrumentation output export __DEBUG__:
  60. ```
  61. $ DEBUG=send node app
  62. ```
  63. ## Running tests
  64. ```
  65. $ npm install
  66. $ make test
  67. ```
  68. ## License
  69. (The MIT License)
  70. Copyright (c) 2012 TJ Holowaychuk <tj@vision-media.ca>
  71. Permission is hereby granted, free of charge, to any person obtaining
  72. a copy of this software and associated documentation files (the
  73. 'Software'), to deal in the Software without restriction, including
  74. without limitation the rights to use, copy, modify, merge, publish,
  75. distribute, sublicense, and/or sell copies of the Software, and to
  76. permit persons to whom the Software is furnished to do so, subject to
  77. the following conditions:
  78. The above copyright notice and this permission notice shall be
  79. included in all copies or substantial portions of the Software.
  80. THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
  81. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  82. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  83. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  84. CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  85. TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  86. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.