Express.js Pass-Through Directory

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 is the author of Advanced Microservices and is a prolific public speaker with a passion for reducing complex problems into simple language and diagrams. His career includes working at Fortune 50's in the Midwest, co-founding a successful startup, and everything in between.