Skip to content
tezvyn:
Menu

Serving static files in Express

Source: interviewEasyHow cards are made

Summary

knowing the built-in static middleware.

Key points

app.use with express.static pointing at the public directory, files served relative to that root, often combined with an absolute path.

What's really being asked

This checks whether you know Express ships a built-in solution for serving assets rather than requiring manual routes, and whether you handle the path robustly.

The full answer

Express provides the express.static middleware for serving files from a folder. You register it with app.use(express.static('public')), and Express then serves any file in that directory at a URL path relative to the directory root: public/css/style.css becomes available at /css/style.css, with no extra routing code. Because a relative path is resolved against the process current working directory, which can vary by how you launch the app, the robust form uses an absolute path: app.use(express.static(path.join(__dirname, 'public'))). You can mount it under a virtual prefix, such as app.use('/static', express.static('public')), so files appear under /static. The middleware also sets caching headers and handles content types automatically.

The mistakes people make

Writing an individual app.get route for each asset, which is unmaintainable. Using a bare relative path and being surprised when files are not found because the working directory differs. Confusing the filesystem path argument with the URL prefix.

What usually comes next

How do you mount static files under a URL prefix. Why use __dirname here. How would you set cache-control or maxAge on static assets.

A concrete example

With app.use(express.static(path.join(dirname, 'public'))) and a file at public/js/app.js, a browser request to /js/app.js returns that script with the correct Content-Type, no per-file route required. Adding app.use('/assets', express.static(path.join(dirname, 'public'))) would instead expose it at /assets/js/app.js.

Interview question

Why is express.static(path.join(__dirname, 'public')) preferred over express.static('public')?

  • a.Only the absolute form enables HTTP caching headers
  • b.The absolute form serves files faster from disk
  • c.A bare relative path resolves against the variable working directory and can breakCorrect
  • d.The relative form exposes the public folder to directory traversal
Why?

A relative path is resolved against the current working directory, which varies by launch context, so the absolute __dirname-based path is reliable. Both forms set caching headers and serve at the same speed.

Just read this? Test yourself on what you have been reading.

Read the original → expressjs.com

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on express — each one lists the topics its interview covers.

See open roles