3 [![NPM Version][npm-image]][npm-url]
\r
4 [![NPM Downloads][downloads-image]][downloads-url]
\r
5 [![Linux Build][travis-image]][travis-url]
\r
6 [![Windows Build][appveyor-image]][appveyor-url]
\r
7 [![Test Coverage][coveralls-image]][coveralls-url]
\r
8 [![Gratipay][gratipay-image]][gratipay-url]
\r
10 Serves pages that contain directory listings for a given path.
\r
15 $ npm install serve-index
\r
21 var serveIndex = require('serve-index')
\r
24 ### serveIndex(path, options)
\r
26 Returns middlware that serves an index of the directory in the given `path`.
\r
28 The `path` is based off the `req.url` value, so a `req.url` of `'/some/dir`
\r
29 with a `path` of `'public'` will look at `'public/some/dir'`. If you are using
\r
30 something like `express`, you can change the URL "base" with `app.use` (see
\r
31 the express example).
\r
35 Serve index accepts these properties in the options object.
\r
39 Apply this filter function to files. Defaults to `false`. The `filter` function
\r
40 is called for each file, with the signature `filter(filename, index, files, dir)`
\r
41 where `filename` is the name of the file, `index` is the array index, `files` is
\r
42 the array of files and `dir` is the absolute path the file is located (and thus,
\r
43 the directory the listing is for).
\r
47 Display hidden (dot) files. Defaults to `false`.
\r
51 Display icons. Defaults to `false`.
\r
55 Optional path to a CSS stylesheet. Defaults to a built-in stylesheet.
\r
59 Optional path to an HTML template or a function that will render a HTML
\r
60 string. Defaults to a built-in template.
\r
62 When given a string, the string is used as a file path to load and then the
\r
63 following tokens are replaced in templates:
\r
65 * `{directory}` with the name of the directory.
\r
66 * `{files}` with the HTML of an unordered list of file links.
\r
67 * `{linked-path}` with the HTML of a link to the directory.
\r
68 * `{style}` with the specified stylesheet and embedded images.
\r
70 When given as a function, the function is called as `template(locals, callback)`
\r
71 and it needs to invoke `callback(error, htmlString)`. The following are the
\r
74 * `directory` is the directory being displayed (where `/` is the root).
\r
75 * `displayIcons` is a Boolean for if icons should be rendered or not.
\r
76 * `fileList` is a sorted array of files in the directory. The array contains
\r
77 objects with the following properties:
\r
78 - `name` is the relative name for the file.
\r
79 - `stat` is a `fs.Stats` object for the file.
\r
80 * `path` is the full filesystem path to `directory`.
\r
81 * `style` is the default stylesheet or the contents of the `stylesheet` option.
\r
82 * `viewName` is the view name provided by the `view` option.
\r
86 Display mode. `tiles` and `details` are available. Defaults to `tiles`.
\r
90 ### Serve directory indexes with vanilla node.js http server
\r
93 var finalhandler = require('finalhandler')
\r
94 var http = require('http')
\r
95 var serveIndex = require('serve-index')
\r
96 var serveStatic = require('serve-static')
\r
98 // Serve directory indexes for public/ftp folder (with icons)
\r
99 var index = serveIndex('public/ftp', {'icons': true})
\r
101 // Serve up public/ftp folder files
\r
102 var serve = serveStatic('public/ftp')
\r
105 var server = http.createServer(function onRequest(req, res){
\r
106 var done = finalhandler(req, res)
\r
107 serve(req, res, function onNext(err) {
\r
108 if (err) return done(err)
\r
109 index(req, res, done)
\r
114 server.listen(3000)
\r
117 ### Serve directory indexes with express
\r
120 var express = require('express')
\r
121 var serveIndex = require('serve-index')
\r
123 var app = express()
\r
125 // Serve URLs like /ftp/thing as public/ftp/thing
\r
126 app.use('/ftp', serveIndex('public/ftp', {'icons': true}))
\r
132 [MIT](LICENSE). The [Silk](http://www.famfamfam.com/lab/icons/silk/) icons
\r
133 are created by/copyright of [FAMFAMFAM](http://www.famfamfam.com/).
\r
135 [npm-image]: https://img.shields.io/npm/v/serve-index.svg
\r
136 [npm-url]: https://npmjs.org/package/serve-index
\r
137 [travis-image]: https://img.shields.io/travis/expressjs/serve-index/master.svg?label=linux
\r
138 [travis-url]: https://travis-ci.org/expressjs/serve-index
\r
139 [appveyor-image]: https://img.shields.io/appveyor/ci/dougwilson/serve-index/master.svg?label=windows
\r
140 [appveyor-url]: https://ci.appveyor.com/project/dougwilson/serve-index
\r
141 [coveralls-image]: https://img.shields.io/coveralls/expressjs/serve-index/master.svg
\r
142 [coveralls-url]: https://coveralls.io/r/expressjs/serve-index?branch=master
\r
143 [downloads-image]: https://img.shields.io/npm/dm/serve-index.svg
\r
144 [downloads-url]: https://npmjs.org/package/serve-index
\r
145 [gratipay-image]: https://img.shields.io/gratipay/dougwilson.svg
\r
146 [gratipay-url]: https://www.gratipay.com/dougwilson/
\r