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
Start learning this pattern below
Jump into concepts and practice - no test required
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.
Practice
express.static in a Node.js server?Solution
Step 1: Understand the role of
This middleware is designed to serve files such as images, CSS, and JavaScript directly to the browser.express.staticStep 2: Compare with other server tasks
Database connections, dynamic page creation, and authentication are handled by other parts of the server, notexpress.static.Final Answer:
To serve static files like images, CSS, and JavaScript to users -> Option DQuick Check:
Serving static files = A [OK]
- Confusing static file serving with database handling
- Thinking express.static creates dynamic content
- Assuming express.static manages user login
public using Express?Solution
Step 1: Recall Express static middleware syntax
The correct way is to callapp.use(express.static('folderName'))to serve static files.Step 2: Check each option
Only app.use(express.static('public')) uses the correct method and syntax. Others are invalid or do not exist in Express.Final Answer:
app.use(express.static('public')) -> Option BQuick Check:
Correct static serving syntax = B [OK]
- Using app.static instead of app.use(express.static)
- Calling express.use.static which is invalid
- Trying app.getStatic which does not exist
const express = require('express');
const app = express();
app.use(express.static('assets'));
app.listen(3000);What URL path would serve the file
assets/logo.png?Solution
Step 1: Understand express.static path mapping
When serving static files from folder 'assets', the files are accessible at the root URL path, so 'logo.png' is at '/logo.png'.Step 2: Analyze each URL option
http://localhost:3000/logo.png matches the correct URL path. Options A, B, and C incorrectly add folder names to the URL path.Final Answer:
http://localhost:3000/logo.png -> Option CQuick Check:
Static folder files appear at root URL path = D [OK]
- Adding folder name to URL path incorrectly
- Assuming static files need /static or /public prefix
- Confusing folder name with URL path
static folder:const express = require('express');
const app = express();
app.use(express.static.static('static'));
app.listen(3000);Solution
Step 1: Check the express.static usage
The code incorrectly callsexpress.static.static. The correct call isexpress.static.Step 2: Verify other parts
App.listen is present, folder name can be relative, and folder name does not have to be 'public'.Final Answer:
Incorrect use of express.static.static instead of express.static -> Option AQuick Check:
Use express.static, not express.static.static [OK]
- Adding extra .static after express.static
- Thinking folder must be named 'public'
- Believing folder path must be absolute
public folder but under the URL path prefix /static. Which code correctly achieves this?Solution
Step 1: Understand URL prefix with express.static
To serve static files under a URL prefix, pass the prefix as the first argument toapp.use, then the static middleware.Step 2: Evaluate each option
app.use('/static', express.static('public')) correctly usesapp.use('/static', express.static('public')). Others either misuse paths or methods.Final Answer:
app.use('/static', express.static('public')) -> Option AQuick Check:
URL prefix with app.use('/prefix', express.static(folder)) = A [OK]
- Putting URL prefix inside express.static argument
- Using app.get instead of app.use for static files
- Mixing folder and URL prefix names
