serve-directory

Serve directory listings

Usage no npm install needed!

<script type="module">
  import serveDirectory from 'https://cdn.skypack.dev/serve-directory';
</script>

README

serve-directory

Build Status devDependencies Coveralls github LGTM Grade Issues Issues GitHub last commit GitHub Release Date

npm npm dependencies Snyk Vulnerabilities for npm package version install size

styled with prettier MIT license

Serves pages that contain directory listings for a given path. forked from expressjs/serve-directory

Installation

yarn add serve-directory

Usage

import serveDirectory from 'serve-directory'

const directory = serveDirectory('wwwroot', options)

Serve directory indexes with vanilla node.js http server

see example.js

serveDirectory(path, options)

Returns middleware 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(Object)

serveDirectory accepts these properties in the options object.

example

defaults

{
  imports: {},
  hidden: false,
  relative: true,
  process: [
    {
      accept: 'text/html',
      render: 'lib/directory.jst'
    },
    {
      accept: 'text/plain',
      render: ({fileNames}) => fileNames.join(EOL) + EOL,
    },
    {
      accept: 'application/json',
      render: ({fileNames}) => JSON.stringify(fileNames),
    },
  ]
}

options.imports(Object)

An object to import into the template as free variables., see lodash.template

by default some useful functions will import automatically

see utils/index.js

options.hidden(Boolean)

Show hidden files(file/folder start with .) , defaults to false.

options.relative(Object)

Use relative url , default true.

options.process(Array)

Array list how data should be handled

options.accept

Array or String split with ,

space will be trim

options.render

by default we use a compiled lodash.template function to render data

see lodash.template

  • String

    a path to a template file a template string

  • Function

    a custom render function

  • falsy value

    remove default render function

data

data pass to the render function

  • path String

    physical path

  • pathname String

    decoded request pathname

  • url URL

    request URL object

  • method String

    request method

  • responseType String

    response mine-type / content-type

  • directory fs.Stats

    directory stats with additional info path pathname url

  • files Array<fs.Stats>

    directory files stats with additional info name ext type url

  • fileNames Array<String>

    directory files but name only

License

MIT © fisker Cheung