Express.js - any way to display a file/dir listing?
There's a brand new default Connect middleware named directory
(source) for directory listings. It has a lot of style and has a client-side search box.
var express = require('express')
, app = express.createServer();
app.configure(function() {
var hourMs = 1000*60*60;
app.use(express.static(__dirname + '/public', { maxAge: hourMs }));
app.use(express.directory(__dirname + '/public'));
app.use(express.errorHandler());
});
app.listen(8080);
The following code will serve both directory and files
var serveIndex = require('serve-index');
app.use('/p', serveIndex(path.join(__dirname, 'public')));
app.use('/p', express.static(path.join(__dirname, 'public')));
As of Express 4.x, the directory middleware is no longer bundled with express. You'll want to download the npm module serve-index.
Then, for example, to display the file/dir listings in a directory at the root of the app called videos
would look like:
var serveIndex = require('serve-index');
app.use(express.static(__dirname + "/"))
app.use('/videos', serveIndex(__dirname + '/videos'));