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.

165 lines
4.5 KiB

7 years ago
  1. # serve-static
  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. [![Gratipay][gratipay-image]][gratipay-url]
  7. ## Install
  8. ```sh
  9. $ npm install serve-static
  10. ```
  11. ## API
  12. ```js
  13. var serveStatic = require('serve-static')
  14. ```
  15. ### serveStatic(root, options)
  16. Create a new middleware function to serve files from within a given root
  17. directory. The file to serve will be determined by combining `req.url`
  18. with the provided root directory. When a file is not found, instead of
  19. sending a 404 response, this module will instead call `next()` to move on
  20. to the next middleware, allowing for stacking and fall-backs.
  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 call `next()`.
  33. ##### etag
  34. Enable or disable etag generation, defaults to true.
  35. ##### extensions
  36. Set file extension fallbacks. When set, if a file is not found, the given
  37. extensions will be added to the file name and search for. The first that
  38. exists will be served. Example: `['html', 'htm']`.
  39. The default value is `false`.
  40. ##### index
  41. By default this module will send "index.html" files in response to a request
  42. on a directory. To disable this set `false` or to supply a new index pass a
  43. string or an array 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. This
  49. can also be a string accepted by the [ms](https://www.npmjs.org/package/ms#readme)
  50. module.
  51. ##### redirect
  52. Redirect to trailing "/" when the pathname is a dir. Defaults to `true`.
  53. ##### setHeaders
  54. Function to set custom headers on response. Alterations to the headers need to
  55. occur synchronously. The function is called as `fn(res, path, stat)`, where
  56. the arguments are:
  57. - `res` the response object
  58. - `path` the file path that is being sent
  59. - `stat` the stat object of the file that is being sent
  60. ## Examples
  61. ### Serve files with vanilla node.js http server
  62. ```js
  63. var finalhandler = require('finalhandler')
  64. var http = require('http')
  65. var serveStatic = require('serve-static')
  66. // Serve up public/ftp folder
  67. var serve = serveStatic('public/ftp', {'index': ['index.html', 'index.htm']})
  68. // Create server
  69. var server = http.createServer(function(req, res){
  70. var done = finalhandler(req, res)
  71. serve(req, res, done)
  72. })
  73. // Listen
  74. server.listen(3000)
  75. ```
  76. ### Serve all files as downloads
  77. ```js
  78. var contentDisposition = require('content-disposition')
  79. var finalhandler = require('finalhandler')
  80. var http = require('http')
  81. var serveStatic = require('serve-static')
  82. // Serve up public/ftp folder
  83. var serve = serveStatic('public/ftp', {
  84. 'index': false,
  85. 'setHeaders': setHeaders
  86. })
  87. // Set header to force download
  88. function setHeaders(res, path) {
  89. res.setHeader('Content-Disposition', contentDisposition(path))
  90. }
  91. // Create server
  92. var server = http.createServer(function(req, res){
  93. var done = finalhandler(req, res)
  94. serve(req, res, done)
  95. })
  96. // Listen
  97. server.listen(3000)
  98. ```
  99. ### Serving using express
  100. ```js
  101. var connect = require('connect')
  102. var serveStatic = require('serve-static')
  103. var app = connect()
  104. app.use(serveStatic('public/ftp', {'index': ['default.html', 'default.htm']}))
  105. app.listen(3000)
  106. ```
  107. ## License
  108. [MIT](LICENSE)
  109. [npm-image]: https://img.shields.io/npm/v/serve-static.svg?style=flat
  110. [npm-url]: https://npmjs.org/package/serve-static
  111. [travis-image]: https://img.shields.io/travis/expressjs/serve-static.svg?style=flat
  112. [travis-url]: https://travis-ci.org/expressjs/serve-static
  113. [coveralls-image]: https://img.shields.io/coveralls/expressjs/serve-static.svg?style=flat
  114. [coveralls-url]: https://coveralls.io/r/expressjs/serve-static
  115. [downloads-image]: https://img.shields.io/npm/dm/serve-static.svg?style=flat
  116. [downloads-url]: https://npmjs.org/package/serve-static
  117. [gratipay-image]: https://img.shields.io/gratipay/dougwilson.svg?style=flat
  118. [gratipay-url]: https://gratipay.com/dougwilson/