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