Over a million developers have joined DZone.

Using the Directory-serving Middleware in Express

DZone's Guide to

Using the Directory-serving Middleware in Express

· Web Dev Zone
Free Resource

Get the senior executive’s handbook of important trends, tips, and strategies to compete and win in the digital economy.

If you peruse the docs for ExpressJS (as I do every Sunday), you might notice this little gem called Directory:

Directory serving middleware that serves the given path. This middleware may be paired with static() to serve files, providing a full-featured file browser.

I was curious how this worked since both Apache and IIS have this built in as well. I whipped up a quick Express application (remember you can create one with the Express command line program) and tried the feature out.

First, note that the docs use this as an example:


When I first tried this with an application it didn't work. I had forgotten to include a path with the app.use statement. This made it default to /, but I was always using that path with the out-of-the-box code. I quickly changed it to this:


This let me map /dropbox in the browser path to my local Dropbox install. Here's how this is rendered by Express*:

As you can see it is nice, if somewhat minimal. You can click to browse into subfolders:

The search field has a nice highlight effect, but is case-sensitive which seems like an odd choice:

There are a few options you can use with the Directory middleware. One is icon support, but in my testing this was very poorly implemented. As far as I could tell it only supported PDFs. I appreciate the Adobe-love there, but it seems odd that other file types weren't recognized.

Here is an example of how you would enable it:


And the result:

Yet another option is the ability to filter the results. The docs don't tell you how to use this. I supplied a function to my options object and simply did a console.log(arguments) to see what was passed.

From what I could see there were three arguments. The first is the name of the item. The second is the index (i.e., the number representing which item in the list this is--zero based). The third item is the entire list. Again, oddly, you aren't told if the item is a file or a directory. Since you know what directory you're working with you could simply check this yourself but it would certainly be helpful if the middleware provided this. I wrote this function to show directories and PDF files. Note that my "directory logic" is a hack. I simply see if the name has a period in it. This is not a real test!

		filter:function(file,pos,list) {
			return (file.indexOf('.') === -1 || file.indexOf('pdf') >= 1);

Finally, don't forget that if you want to actually serve the files you have to combine with it a static call as well:


* As an FYI, the Directory middleware is actually provided by Connect, which Express builds upon. This tends to confuse me at times.

Read this guide to learn everything you need to know about RPA, and how it can help you manage and automate your processes.


Published at DZone with permission of Raymond Camden, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}