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 Serves pages that contain directory listings for a given path.
15 $ npm install serve-index
21 var serveIndex = require('serve-index')
24 ### serveIndex(path, options)
26 Returns middlware that serves an index of the directory in the given `path`.
28 The `path` is based off the `req.url` value, so a `req.url` of `'/some/dir`
29 with a `path` of `'public'` will look at `'public/some/dir'`. If you are using
30 something like `express`, you can change the URL "base" with `app.use` (see
35 Serve index accepts these properties in the options object.
39 Apply this filter function to files. Defaults to `false`. The `filter` function
40 is called for each file, with the signature `filter(filename, index, files, dir)`
41 where `filename` is the name of the file, `index` is the array index, `files` is
42 the array of files and `dir` is the absolute path the file is located (and thus,
43 the directory the listing is for).
47 Display hidden (dot) files. Defaults to `false`.
51 Display icons. Defaults to `false`.
55 Optional path to a CSS stylesheet. Defaults to a built-in stylesheet.
59 Optional path to an HTML template. Defaults to a built-in template.
61 The following tokens are replaced in templates:
63 * `{directory}` with the name of the directory.
64 * `{files}` with the HTML of an unordered list of file links.
65 * `{linked-path}` with the HTML of a link to the directory.
66 * `{style}` with the specified stylesheet and embedded images.
70 Display mode. `tiles` and `details` are available. Defaults to `tiles`.
74 ### Serve directory indexes with vanilla node.js http server
77 var finalhandler = require('finalhandler')
78 var http = require('http')
79 var serveIndex = require('serve-index')
80 var serveStatic = require('serve-static')
82 // Serve directory indexes for public/ftp folder (with icons)
83 var index = serveIndex('public/ftp', {'icons': true})
85 // Serve up public/ftp folder files
86 var serve = serveStatic('public/ftp')
89 var server = http.createServer(function onRequest(req, res){
90 var done = finalhandler(req, res)
91 serve(req, res, function onNext(err) {
92 if (err) return done(err)
101 ### Serve directory indexes with express
104 var express = require('express')
105 var serveIndex = require('serve-index')
109 // Serve URLs like /ftp/thing as public/ftp/thing
110 app.use('/ftp', serveIndex('public/ftp', {'icons': true}))
116 [MIT](LICENSE). The [Silk](http://www.famfamfam.com/lab/icons/silk/) icons
117 are created by/copyright of [FAMFAMFAM](http://www.famfamfam.com/).
119 [npm-image]: https://img.shields.io/npm/v/serve-index.svg
120 [npm-url]: https://npmjs.org/package/serve-index
121 [travis-image]: https://img.shields.io/travis/expressjs/serve-index/master.svg?label=linux
122 [travis-url]: https://travis-ci.org/expressjs/serve-index
123 [appveyor-image]: https://img.shields.io/appveyor/ci/dougwilson/serve-index/master.svg?label=windows
124 [appveyor-url]: https://ci.appveyor.com/project/dougwilson/serve-index
125 [coveralls-image]: https://img.shields.io/coveralls/expressjs/serve-index/master.svg
126 [coveralls-url]: https://coveralls.io/r/expressjs/serve-index?branch=master
127 [downloads-image]: https://img.shields.io/npm/dm/serve-index.svg
128 [downloads-url]: https://npmjs.org/package/serve-index
129 [gratipay-image]: https://img.shields.io/gratipay/dougwilson.svg
130 [gratipay-url]: https://www.gratipay.com/dougwilson/