Serving static files lets your web server send images, styles, and scripts to users. It helps show pictures and design on websites.
Serving static files in Node.js
import express from 'express'; const app = express(); app.use(express.static('public')); app.listen(3000);
The express.static middleware serves files from the folder you specify.
Files inside the folder are accessible by their path in the URL.
public folder at the root URL.app.use(express.static('public'))assets folder but under the URL path /static.app.use('/static', express.static('assets'))
app.use(express.static('public', { dotfiles: 'ignore' }))
This program starts a server on port 3000. It sends any file inside the public folder when requested by the browser.
For example, if public/image.png exists, visiting http://localhost:3000/image.png will show that image.
import express from 'express'; const app = express(); // Serve files from 'public' folder app.use(express.static('public')); app.listen(3000, () => { console.log('Server running on http://localhost:3000'); });
Make sure the folder you serve exists and has the files you want to share.
Static files are sent as-is, so update them carefully to avoid broken pages.
Use meaningful folder names like public or assets to keep your project organized.
Serving static files lets your server send images, CSS, and scripts to users.
Use express.static middleware to serve files from a folder.
Files inside the folder are accessible by their path in the browser URL.