Express.js Pass-Through Directory

Support this website by purchasing prints of my photographs! Check them out here.
DEPRECATED: This post may no longer be relevant or contain industry best-practices.

Would you like to server up a directory of static content using express.js? Then look no further. This URL will match all requests for the top level assets directory, and will server up a file from the local assets directory relative to where the script was executed from. You can independently change the name of the folder on disk and the URL without interfering with each other.

app.get('/assets/*', function (req, res) {
    res.sendfile(__dirname + '/assets/' + req.params[0]);

Unfortunately, I have no idea if this is secure, or if it is a best practice. Please leave feedback if you have any better suggestions.

Tags: #nodejs
Thomas Hunter II Avatar

Thomas has contributed to dozens of enterprise Node.js services and has worked for a company dedicated to securing Node.js. He has spoken at several conferences on Node.js and JavaScript and is an O'Reilly published author.