Express.js Pass-Through Directory

Looking for a better way to manage your org's technical proposals?
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 Hunter II is a Software Engineer with 18+ years building scalable, production-grade systems. Deep expertise in Node.js, observability, and developer tooling, with a strong track record of driving architectural decisions, mentoring engineers, and improving customer facing technical documentation. Author of 5 books (including O'Reilly), certified by the OpenJS Foundation, and frequent international conference speaker.