Express.js - any way to display a file/dir listing?

node.jsExpress

node.js Problem Overview


With Express.js is there a way to display a file/dir listing like apache does when you access the URL of a directory which doesn't have a index file - so it displays a listing of all that directories contents?

Is there an extension or package that does this which I don't know of? Or will I have to code this myself?

Cheers guys, you rock! :)

node.js Solutions


Solution 1 - node.js

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'));

Solution 2 - node.js

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);

Solution 3 - node.js

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')));

Solution 4 - node.js

This will do the work for you: (new version of express requires separate middleware). E.g. you put your files under folder 'files' and you want the url to be '/public'

var express = require('express');
var serveIndex = require('serve-index');
var app = express();

app.use('/public', serveIndex('files')); // shows you the file list
app.use('/public', express.static('files')); // serve the actual files

Solution 5 - node.js

Built-in NodeJS module fs gives a lot of fine-grained options

const fs = require('fs')

router.get('*', (req, res) => {
    const fullPath = process.cwd() + req.path //(not __dirname)
    const dir = fs.opendirSync(fullPath)
    let entity
    let listing = []
    while((entity = dir.readSync()) !== null) {
        if(entity.isFile()) {
            listing.push({ type: 'f', name: entity.name })
        } else if(entity.isDirectory()) {
            listing.push({ type: 'd', name: entity.name })
        }
    }
    dir.closeSync()
    res.send(listing)
})

Please make sure to read up on path-traversal security vulnerabilities.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbaluptonView Question on Stackoverflow
Solution 1 - node.jsjbllView Answer on Stackoverflow
Solution 2 - node.jsyonranView Answer on Stackoverflow
Solution 3 - node.jsTalespin_KitView Answer on Stackoverflow
Solution 4 - node.jsXinView Answer on Stackoverflow
Solution 5 - node.jsMatteljayView Answer on Stackoverflow