DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Securing Your Software Supply Chain with JFrog and Azure
Register Today

Trending

  • A Complete Guide to AWS File Handling and How It Is Revolutionizing Cloud Storage
  • Integrate Cucumber in Playwright With Java
  • Five Java Books Beginners and Professionals Should Read
  • Step Into Serverless Computing

Trending

  • A Complete Guide to AWS File Handling and How It Is Revolutionizing Cloud Storage
  • Integrate Cucumber in Playwright With Java
  • Five Java Books Beginners and Professionals Should Read
  • Step Into Serverless Computing
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Using the Directory-serving Middleware in Express

Using the Directory-serving Middleware in Express

Raymond Camden user avatar by
Raymond Camden
·
Aug. 13, 13 · Interview
Like (0)
Save
Tweet
Share
3.93K Views

Join the DZone community and get the full member experience.

Join For Free

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:

app.use(express.directory('public'));

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:

app.use('/dropbox',express.directory('/Users/ray/Dropbox'));

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:

app.use('/dropbox',express.directory('/Users/ray/Dropbox',{icons:true}));

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!

app.use('/dropbox',express.directory('/Users/ray/Dropbox',
  {icons:true,
		filter:function(file,pos,list) {
			console.log(arguments);
			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:

app.use('/dropbox',express.static('/Users/ray/Dropbox'));

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

Express Middleware

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

Opinions expressed by DZone contributors are their own.

Trending

  • A Complete Guide to AWS File Handling and How It Is Revolutionizing Cloud Storage
  • Integrate Cucumber in Playwright With Java
  • Five Java Books Beginners and Professionals Should Read
  • Step Into Serverless Computing

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: