• Stars
    star
    435
  • Rank 96,152 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 10 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Serve directory listings

serve-index

NPM Version NPM Downloads Linux Build Windows Build Test Coverage

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.

More Repositories

1

express

Fast, unopinionated, minimalist web framework for node.
JavaScript
63,539
star
2

multer

Node.js middleware for handling `multipart/form-data`.
JavaScript
11,285
star
3

morgan

HTTP request logger middleware for node.js
JavaScript
7,790
star
4

session

Simple session middleware for Express
JavaScript
6,163
star
5

cors

Node.js CORS middleware
JavaScript
5,961
star
6

body-parser

Node.js body parsing middleware
JavaScript
5,376
star
7

expressjs.com

HTML
5,138
star
8

compression

Node.js compression middleware
JavaScript
2,722
star
9

csurf

CSRF token middleware
2,299
star
10

cookie-parser

Parse HTTP request cookies
JavaScript
1,907
star
11

generator

Express' application generator
JavaScript
1,803
star
12

serve-static

Serve static files
JavaScript
1,368
star
13

cookie-session

Simple cookie-based session middleware
JavaScript
1,104
star
14

vhost

virtual domain hosting
JavaScript
758
star
15

serve-favicon

favicon serving middleware
JavaScript
620
star
16

method-override

Override HTTP verbs.
JavaScript
614
star
17

response-time

Response time header for node.js
JavaScript
458
star
18

errorhandler

Development-only error handler middleware
JavaScript
423
star
19

express-paginate

Paginate middleware
JavaScript
417
star
20

connect-multiparty

connect middleware for multiparty
JavaScript
347
star
21

express-namespace

Adds namespaced routing capabilities to Express
JavaScript
345
star
22

timeout

Request timeout middleware for Connect/Express
JavaScript
312
star
23

express-expose

Expose raw js, objects, and functions to the client-side (awesome for sharing utils, settings, current user data etc)
JavaScript
299
star
24

basic-auth-connect

Basic auth middleware for node and connect
JavaScript
129
star
25

domain-middleware

`uncaughtException` middleware for connect, base on `domain` module.
JavaScript
101
star
26

api-error-handler

Express error handlers for JSON APIs
JavaScript
100
star
27

flash

JavaScript
92
star
28

restful-router

Simple RESTful url router.
JavaScript
86
star
29

urlrouter

http url router, `connect` missing router middleware
JavaScript
59
star
30

discussions

Public discussions for the Express.js organization
55
star
31

vhostess

virtual host sub-domain mapping
JavaScript
24
star
32

connect-markdown

Auto convert markdown to html for connect.
JavaScript
20
star
33

expressjs.github.io

16
star
34

connect-rid

connect request id middleware
JavaScript
10
star
35

routification

DEPRECATED
JavaScript
10
star
36

mime-extended

DEPRECATED - Please use mime-types instead.
JavaScript
7
star
37

statusboard

A project status board for the Express community
6
star
38

.github

5
star
39

set-type

DEPRECATED - Please use mime-types instead.
JavaScript
5
star
40

security-wg

Express.js Security Working Group
4
star
41

Admin

Admin repository for the Express Organization, including pillarjs and jshttp
2
star