serve-index
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
accepts | 1.2.13 | 4.7 kB | MIT | prod | 1 |
batch | 0.5.3 | 3.03 kB | MIT | prod | |
debug | 2.2.0 | 10.05 kB | MIT | prod | 1 1 1 1 |
escape-html | 1.0.3 | 1.87 kB | MIT | prod | |
http-errors | 1.3.1 | 3.42 kB | MIT | prod | |
mime-types | 2.1.35 | 5.46 kB | MIT | prod | |
parseurl | 1.3.3 | 3.86 kB | MIT | prod |
Serve-index is a highly useful Node.js module available through the npm registry. This potent module provides functionality to serve pages that contain directory listings for a specified path. By doing so, it provides an organized view of the files within a certain directory, making navigation much easier, particularly in larger projects.
To implement serve-index into your project, first, you need to install it using the npm install command npm install serve-index
. Once the installation is complete, you can require it in your file with var serveIndex = require('serve-index')
.
Using serve-index with a vanilla Node.js HTTP server might look something like this:
var finalhandler = require('finalhandler');
var http = require('http');
var serveIndex = require('serve-index');
var serveStatic = require('serve-static');
// Serve directory indexes for the 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);
If you're using Express, this module can clean up your directory serving significantly. Here's an example of how it can look with Express:
var express = require('express');
var serveIndex = require('serve-index');
var app = express();
// Serve URLs like /ftp/thing as public/ftp/thing
app.use('/ftp', express.static('public/ftp'), serveIndex('public/ftp', {'icons': true}));
// Listen
app.listen(3000);
The documentation for serve-index can be found within the readme on the module's GitHub page at git+https://github.com/expressjs/serve-index.git. This comprehensive documentation runs through installation, options for customizing behavior, API details, and even provides some useful examples showing how to use serve-index with different server configurations.