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.

154 lines
5.0 KiB

4 years ago
  1. # serve-index
  2. [![NPM Version][npm-image]][npm-url]
  3. [![NPM Downloads][downloads-image]][downloads-url]
  4. [![Linux Build][travis-image]][travis-url]
  5. [![Windows Build][appveyor-image]][appveyor-url]
  6. [![Test Coverage][coveralls-image]][coveralls-url]
  7. [![Gratipay][gratipay-image]][gratipay-url]
  8. Serves pages that contain directory listings for a given path.
  9. ## Install
  10. This is a [Node.js](https://nodejs.org/en/) module available through the
  11. [npm registry](https://www.npmjs.com/). Installation is done using the
  12. [`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally):
  13. ```sh
  14. $ npm install serve-index
  15. ```
  16. ## API
  17. ```js
  18. var serveIndex = require('serve-index')
  19. ```
  20. ### serveIndex(path, options)
  21. Returns middlware that serves an index of the directory in the given `path`.
  22. The `path` is based off the `req.url` value, so a `req.url` of `'/some/dir`
  23. with a `path` of `'public'` will look at `'public/some/dir'`. If you are using
  24. something like `express`, you can change the URL "base" with `app.use` (see
  25. the express example).
  26. #### Options
  27. Serve index accepts these properties in the options object.
  28. ##### filter
  29. Apply this filter function to files. Defaults to `false`. The `filter` function
  30. is called for each file, with the signature `filter(filename, index, files, dir)`
  31. where `filename` is the name of the file, `index` is the array index, `files` is
  32. the array of files and `dir` is the absolute path the file is located (and thus,
  33. the directory the listing is for).
  34. ##### hidden
  35. Display hidden (dot) files. Defaults to `false`.
  36. ##### icons
  37. Display icons. Defaults to `false`.
  38. ##### stylesheet
  39. Optional path to a CSS stylesheet. Defaults to a built-in stylesheet.
  40. ##### template
  41. Optional path to an HTML template or a function that will render a HTML
  42. string. Defaults to a built-in template.
  43. When given a string, the string is used as a file path to load and then the
  44. following tokens are replaced in templates:
  45. * `{directory}` with the name of the directory.
  46. * `{files}` with the HTML of an unordered list of file links.
  47. * `{linked-path}` with the HTML of a link to the directory.
  48. * `{style}` with the specified stylesheet and embedded images.
  49. When given as a function, the function is called as `template(locals, callback)`
  50. and it needs to invoke `callback(error, htmlString)`. The following are the
  51. provided locals:
  52. * `directory` is the directory being displayed (where `/` is the root).
  53. * `displayIcons` is a Boolean for if icons should be rendered or not.
  54. * `fileList` is a sorted array of files in the directory. The array contains
  55. objects with the following properties:
  56. - `name` is the relative name for the file.
  57. - `stat` is a `fs.Stats` object for the file.
  58. * `path` is the full filesystem path to `directory`.
  59. * `style` is the default stylesheet or the contents of the `stylesheet` option.
  60. * `viewName` is the view name provided by the `view` option.
  61. ##### view
  62. Display mode. `tiles` and `details` are available. Defaults to `tiles`.
  63. ## Examples
  64. ### Serve directory indexes with vanilla node.js http server
  65. ```js
  66. var finalhandler = require('finalhandler')
  67. var http = require('http')
  68. var serveIndex = require('serve-index')
  69. var serveStatic = require('serve-static')
  70. // Serve directory indexes for public/ftp folder (with icons)
  71. var index = serveIndex('public/ftp', {'icons': true})
  72. // Serve up public/ftp folder files
  73. var serve = serveStatic('public/ftp')
  74. // Create server
  75. var server = http.createServer(function onRequest(req, res){
  76. var done = finalhandler(req, res)
  77. serve(req, res, function onNext(err) {
  78. if (err) return done(err)
  79. index(req, res, done)
  80. })
  81. })
  82. // Listen
  83. server.listen(3000)
  84. ```
  85. ### Serve directory indexes with express
  86. ```js
  87. var express = require('express')
  88. var serveIndex = require('serve-index')
  89. var app = express()
  90. // Serve URLs like /ftp/thing as public/ftp/thing
  91. // The express.static serves the file contents
  92. // The serveIndex is this module serving the directory
  93. app.use('/ftp', express.static('public/ftp'), serveIndex('public/ftp', {'icons': true}))
  94. // Listen
  95. app.listen(3000)
  96. ```
  97. ## License
  98. [MIT](LICENSE). The [Silk](http://www.famfamfam.com/lab/icons/silk/) icons
  99. are created by/copyright of [FAMFAMFAM](http://www.famfamfam.com/).
  100. [npm-image]: https://img.shields.io/npm/v/serve-index.svg
  101. [npm-url]: https://npmjs.org/package/serve-index
  102. [travis-image]: https://img.shields.io/travis/expressjs/serve-index/master.svg?label=linux
  103. [travis-url]: https://travis-ci.org/expressjs/serve-index
  104. [appveyor-image]: https://img.shields.io/appveyor/ci/dougwilson/serve-index/master.svg?label=windows
  105. [appveyor-url]: https://ci.appveyor.com/project/dougwilson/serve-index
  106. [coveralls-image]: https://img.shields.io/coveralls/expressjs/serve-index/master.svg
  107. [coveralls-url]: https://coveralls.io/r/expressjs/serve-index?branch=master
  108. [downloads-image]: https://img.shields.io/npm/dm/serve-index.svg
  109. [downloads-url]: https://npmjs.org/package/serve-index
  110. [gratipay-image]: https://img.shields.io/gratipay/dougwilson.svg
  111. [gratipay-url]: https://www.gratipay.com/dougwilson/