Over a million developers have joined DZone.

Using the Directory-serving Middleware in Express

· Web Dev Zone

Start coding today to experience the powerful engine that drives data application’s development, brought to you in partnership with Qlik.

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.

Create data driven applications in Qlik’s free and easy to use coding environment, brought to you in partnership with Qlik.


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

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}