How can you serve static files using Express?
151
04-Apr-2025
Updated on 04-Apr-2025
ICSM Computer
04-Apr-2025You can serve static files (like HTML, CSS, JS, images, etc.) in Express using the built-in middleware
express.static.Basic Setup:
Example Folder Structure:
With this setup:
http://localhost:3000/index.htmlserves theindex.htmlfile.http://localhost:3000/style.cssserves the CSS file, etc.You can also mount it at a specific route:
Now your files will be accessible like:
http://localhost:3000/static/index.htmlAlso Read: How do you define a route in Express?