serve-index-light
Serve directory listings, based on expressjs/serve-index UI and functionality. Serves pages that contain directory listings for a given path. Can be used without express, as standalone library, provides a lot of customisation possibilities.
Install
$ npm install --save serve-index-light
API
var serveIndex =
serveIndex(options)
Returns function that serves an index of the directory specified by options.
var serveIndex = var http = ;var express = ; var servePublicIndex = // servePublicIndex could be used as standalone serve function: http; // or as express middleware: var app = ;app;app;
Stadalone mode : servePublicIndex(path, basePath, callback)
where
path
- requested pathbasePath
- path to prepend to each generated link. for example you servehttp://example.com/shared/
fromD:/public
, you should usepath = '/'
andbasePath = '/shared'
.function callback(error, data)
- function will be called witherror
anddata
.error
might benull
or object like{ status : 500, message : "invalid path"}
.data
is html string by default.
If requested path not found or it is not a directory, error.code
is set to 404
.
Express middleware mode: servePublicIndex(req, res, next)
. uses req.path
as path and req.baseUrl
as base URL.
in case of 404 error next()
is called, in case of other error next(error)
is called. if no error res.end(data)
is called. this behaviour could be changed by middlewareResultHandler
option.
Options
Some options are functions(behaviour parameterization). all such functions receive full options
object as this
parameter.
baseDir
Required. Path in local file system to serve files from.
fileListProvider
Function to get array of files basing on path. accepts path
and callback
. callback
is function(error, filesArray)
.
filesArray
- array of files at given path, each file represented as fs.Stats object with name
attached to it.
{// return the same file for each path:;}
Default implementation provides list of files and directories in requested path excluding '.' and '..'
showHidden
boolean. when false - does not show files with first symbol .
(unix hidden files). false
by default.
filter
Predicate function to filter or modify file information. Default implementation is:
{ return thisshowHidden || fileInfoname0 != '.';}
if returned value is true
file is preserved in output.
comparator
Function used to compare files during sorting.
{ return aname;}
rowTemplate
Template to render view for 1 file. Default template is table, rowTemplate
is expected to generate one row for this table.
Default template engine is ejs
. Could be overridden by rowTemplateProcessor
.
parameters passed to template:
// all fields from fs.Stats object, name strAtime = atime; strMtime = mtime; strCtime = ctime; strBirthtime = birthtime; strMode = // string like '-rwxrwx---' baseUrl = baseUrl; path = //requested path; href = // full link to this item size = // size of file in bytes or "" for directory sizeHumanReadable = // size like "1.2GB" or "" for directory isDir = // is it file or dir
if you need additional parameters, override rowTemplateProcessor
, filter
or fileListProvider
.
template
Template to render entire view. Default template is http page with table. Default template engine is ejs
. Could be overridden by templateProcessor
.
parameters passed to template:
path : //requested path pathComponents : // array of componens of path baseUrl : baseUrl upPath : // path to '..' directory files : // array of rowTemplate generated strings showUp : path != '/' options : // options object
if you need additional parameters, add it to options, override templateProcessor
or render
.
injectTableHead
String, table head for default template. default value is
NameSizeModifiedAttribute
injectCSS
String, custom css. it will replace default css.
injectJS
String, custom js. will be added at the bottom of body. Empty by default.
injectHead
String, text to append to <head>
tag. Could be used to link external css, for example
injectHead=''
injectBody
String, text to append to <body>
tag. Could be used to link external js, for example
injectBody=''
render
Function, receives path, baseUrl, array of files and callback for result.
{...;}
by default uses rowTemplateProcessor
and templateProcessor
to generate data.
templateProcessor
Function, receives object with data described in template
section, returns generated html string. uses ejs
by default.
{ if !thistemplateCompiled thistemplateCompiled = ejs; return this;}
rowTemplateProcessor
Function, receives object with data described in rowTemplate
section, returns generated html string. uses ejs
by default.
{ if !thisrowTemplateCompiled thisrowTemplateCompiled = ejs; return this;}
middlewareResultHandler
Function, express result handler. Use it to postprocess data instead of sending it to the client. Default implementation is:
{ if !error return res; if errorstatus == 404 return ; return ;}
scanConcurrency
maximum number of parallel IO requests to hard drive. 10 by default.
Examples
simple file server with express
var serveIndex = ;var express = ; { var server = express; server; server; server; return server;} var app = ; app;app;
serve index without express with custom rendering
var serveIndex = ;var http = ; var serve = ; http;
TODO:
- write unit tests