serve-directory
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).
Object
)
options(serveDirectory accepts these properties in the options object.
example
{
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),
},
]
}
Object
)
options.imports(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
Boolean
)
options.hidden(Show hidden files(file/folder start with .
) , defaults to false
.
Object
)
options.relative(Use relative url , default true
.
Array
)
options.process(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
valueremove 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