Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

serve-index

expressjs37.3mMIT1.9.1TypeScript support: definitely-typed

Serve directory listings

readme

serve-index

NPM Version NPM Downloads Linux Build Windows Build Test Coverage Gratipay

Serves pages that contain directory listings for a given path.

Install

This is a Node.js module available through the npm registry. Installation is done using the npm install command:

$ npm install serve-index

API

var serveIndex = require('serve-index')

serveIndex(path, options)

Returns middlware that serves an index of the directory in the given path.

The path is based off the req.url value, so a req.url of '/some/dir with a path of 'public' will look at 'public/some/dir'. If you are using something like express, you can change the URL "base" with app.use (see the express example).

Options

Serve index accepts these properties in the options object.

filter

Apply this filter function to files. Defaults to false. The filter function is called for each file, with the signature filter(filename, index, files, dir) where filename is the name of the file, index is the array index, files is the array of files and dir is the absolute path the file is located (and thus, the directory the listing is for).

hidden

Display hidden (dot) files. Defaults to false.

icons

Display icons. Defaults to false.

stylesheet

Optional path to a CSS stylesheet. Defaults to a built-in stylesheet.

template

Optional path to an HTML template or a function that will render a HTML string. Defaults to a built-in template.

When given a string, the string is used as a file path to load and then the following tokens are replaced in templates:

  • {directory} with the name of the directory.
  • {files} with the HTML of an unordered list of file links.
  • {linked-path} with the HTML of a link to the directory.
  • {style} with the specified stylesheet and embedded images.

When given as a function, the function is called as template(locals, callback) and it needs to invoke callback(error, htmlString). The following are the provided locals:

  • directory is the directory being displayed (where / is the root).
  • displayIcons is a Boolean for if icons should be rendered or not.
  • fileList is a sorted array of files in the directory. The array contains objects with the following properties:
    • name is the relative name for the file.
    • stat is a fs.Stats object for the file.
  • path is the full filesystem path to directory.
  • style is the default stylesheet or the contents of the stylesheet option.
  • viewName is the view name provided by the view option.
view

Display mode. tiles and details are available. Defaults to tiles.

Examples

Serve directory indexes with vanilla node.js http server

var finalhandler = require('finalhandler')
var http = require('http')
var serveIndex = require('serve-index')
var serveStatic = require('serve-static')

// Serve directory indexes for public/ftp folder (with icons)
var index = serveIndex('public/ftp', {'icons': true})

// Serve up public/ftp folder files
var serve = serveStatic('public/ftp')

// Create server
var server = http.createServer(function onRequest(req, res){
  var done = finalhandler(req, res)
  serve(req, res, function onNext(err) {
    if (err) return done(err)
    index(req, res, done)
  })
})

// Listen
server.listen(3000)

Serve directory indexes with express

var express    = require('express')
var serveIndex = require('serve-index')

var app = express()

// Serve URLs like /ftp/thing as public/ftp/thing
// The express.static serves the file contents
// The serveIndex is this module serving the directory
app.use('/ftp', express.static('public/ftp'), serveIndex('public/ftp', {'icons': true}))

// Listen
app.listen(3000)

License

MIT. The Silk icons are created by/copyright of FAMFAMFAM.

changelog

1.9.1 / 2017-09-28

  • deps: accepts@~1.3.4
    • deps: mime-types@~2.1.16
  • deps: debug@2.6.9
  • deps: http-errors@~1.6.2
  • deps: mime-types@~2.1.17
    • Add new mime types
    • deps: mime-db@~1.30.0
  • deps: parseurl@~1.3.2
    • perf: reduce overhead for full URLs
    • perf: unroll the "fast-path" RegExp

1.9.0 / 2017-05-25

  • Set X-Content-Type-Options: nosniff header
  • deps: batch@0.6.1
  • deps: debug@2.6.8
    • Allow colors in workers
    • Deprecated DEBUG_FD environment variable set to 3 or higher
    • Fix DEBUG_MAX_ARRAY_LENGTH
    • Fix error when running under React Native
    • Use same color for same namespace
    • deps: ms@2.0.0
  • deps: http-errors@~1.6.1
  • deps: mime-types@~2.1.15
    • Add new mime types
    • Add audio/mp3

1.8.0 / 2016-06-17

  • Make inline file search case-insensitive
  • deps: accepts@~1.3.3
    • deps: mime-types@~2.1.11
    • deps: negotiator@0.6.1
    • perf: improve header parsing speed
  • deps: http-errors@~1.5.0
    • Use setprototypeof module to replace __proto__ setting
    • deps: inherits@2.0.1
    • deps: statuses@'>= 1.3.0 < 2'
    • perf: enable strict mode
  • deps: mime-types@~2.1.11
    • Add new mime types
    • Update primary extension for audio/mp4
    • deps: mime-db@~1.23.0

1.7.3 / 2016-01-24

  • deps: accepts@~1.2.13
    • deps: mime-types@~2.1.6
  • deps: batch@0.5.3
    • Fix invalid dependency for browserify
  • deps: escape-html@~1.0.3
    • perf: enable strict mode
    • perf: optimize string replacement
    • perf: use faster string coercion
  • deps: mime-types@~2.1.9
    • Add new mime types
  • deps: parseurl@~1.3.1
    • perf: enable strict mode

1.7.2 / 2015-07-30

  • deps: accepts@~1.2.12
    • deps: mime-types@~2.1.4
  • deps: mime-types@~2.1.4
    • Add new mime types

1.7.1 / 2015-07-05

  • deps: accepts@~1.2.10
    • deps: mime-types@~2.1.2
  • deps: mime-types@~2.1.2
    • Add new mime types

1.7.0 / 2015-06-15

  • Accept function value for template option
  • Send non-chunked response for OPTIONS
  • Stat parent directory when necessary
  • Use Date.prototype.toLocaleDateString to format date
  • deps: accepts@~1.2.9
    • deps: mime-types@~2.1.1
    • deps: negotiator@0.5.3
    • perf: avoid argument reassignment & argument slice
    • perf: avoid negotiator recursive construction
    • perf: enable strict mode
    • perf: remove unnecessary bitwise operator
  • deps: escape-html@1.0.2
  • deps: mime-types@~2.1.1
    • Add new mime types
  • perf: enable strict mode
  • perf: remove argument reassignment

1.6.4 / 2015-05-12

  • deps: accepts@~1.2.7
  • deps: debug@~2.2.0
  • deps: mime-types@~2.0.11
    • Add new mime types

1.6.3 / 2015-03-13

  • Properly escape file names in HTML
  • deps: accepts@~1.2.5
    • deps: mime-types@~2.0.10
  • deps: debug@~2.1.3
    • Fix high intensity foreground color for bold
    • deps: ms@0.7.0
  • deps: escape-html@1.0.1
  • deps: mime-types@~2.0.10
    • Add new mime types

1.6.2 / 2015-02-16

  • deps: accepts@~1.2.4
  • deps: http-errors@~1.3.1
    • Construct errors using defined constructors from createError
    • Fix error names that are not identifiers
    • Set a meaningful name property on constructed errors
  • deps: mime-types@~2.0.9
    • Add new mime types
    • deps: mime-db@~1.7.0

1.6.1 / 2015-01-31

  • deps: accepts@~1.2.3
    • deps: mime-types@~2.0.8
  • deps: mime-types@~2.0.8
    • Add new mime types
    • deps: mime-db@~1.6.0

1.6.0 / 2015-01-01

  • Add link to root directory
  • deps: accepts@~1.2.2
  • deps: batch@0.5.2
  • deps: debug@~2.1.1
  • deps: mime-types@~2.0.7
    • Add new mime types
    • Fix missing extensions
    • Fix various invalid MIME type entries
    • Remove example template MIME types
    • deps: mime-db@~1.5.0

1.5.3 / 2014-12-10

  • deps: accepts@~1.1.4
    • deps: mime-types@~2.0.4
  • deps: http-errors@~1.2.8
    • Fix stack trace from exported function
  • deps: mime-types@~2.0.4
    • Add new mime types
    • deps: mime-db@~1.3.0

1.5.2 / 2014-12-03

  • Fix icon name background alignment on mobile view

1.5.1 / 2014-11-22

  • deps: accepts@~1.1.3
    • deps: mime-types@~2.0.3
  • deps: mime-types@~2.0.3
    • Add new mime types
    • deps: mime-db@~1.2.0

1.5.0 / 2014-10-16

  • Create errors with http-errors
  • deps: debug@~2.1.0
    • Implement DEBUG_FD env variable support
  • deps: mime-types@~2.0.2
    • deps: mime-db@~1.1.0

1.4.1 / 2014-10-15

  • deps: accepts@~1.1.2

1.4.0 / 2014-10-03

  • Add dir argument to filter function
  • Support using tokens multiple times

1.3.1 / 2014-10-01

  • Fix incorrect 403 on Windows and Node.js 0.11
  • deps: accepts@~1.1.1

1.3.0 / 2014-09-20

  • Add icon for mkv files
  • Lookup icon by mime type for greater icon support

1.2.1 / 2014-09-05

  • deps: accepts@~1.1.0
  • deps: debug@~2.0.0

1.2.0 / 2014-08-25

  • Add debug messages
  • Resolve relative paths at middleware setup

1.1.6 / 2014-08-10

  • Fix URL parsing
  • deps: parseurl@~1.3.0

1.1.5 / 2014-07-27

  • Fix Content-Length calculation for multi-byte file names
  • deps: accepts@~1.0.7

1.1.4 / 2014-06-20

  • deps: accepts@~1.0.5

1.1.3 / 2014-06-20

  • deps: accepts@~1.0.4
    • use mime-types

1.1.2 / 2014-06-19

1.1.1 / 2014-06-11

1.1.0 / 2014-05-29

  • Fix content negotiation when no Accept header
  • Properly support all HTTP methods
  • Support vanilla node.js http servers
  • Treat ENAMETOOLONG as code 414
  • Use accepts for negotiation

1.0.3 / 2014-05-20

  • Fix error from non-statable files in HTML view

1.0.2 / 2014-04-28

1.0.1 / 2014-03-05

1.0.0 / 2014-03-05

  • Genesis from connect