Express.js express.static() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
express()

Definition

express.static() is built-in middleware that serves files from a folder — CSS, JavaScript, images, fonts, and HTML. It is based on serve-static. Mount it with app.use() and Express maps the request path onto that root directory.

The folder name is not part of the URL. With app.use(express.static('public')), a file at public/css/style.css is available at /css/style.css. Prefer an absolute root (for example path.join(__dirname, 'public')) so the app works no matter which directory you start Node from.

Syntax

Call express.static in your Express.js app. Start with this line.

Express
express.static(root, [options])
CallWhat it does
express.static(root, [options])Primary signature.

Install with npm install express. Create the app with const app = express() before you use this API in an Express.js tutorial project.

Library declarations
Express
const express = require("express");
const app = express();

express.static(root, [options])

Parameters

ParameterDescription
rootRequired. Directory to serve from. Prefer an absolute path with path.join(__dirname, …).
optionsOptional. Behavior and cache settings (see key options below).
options.maxAgeOptional. Cache-Control max-age. Default 0. Strings like '1d' are allowed.
options.etagOptional. Default true. Express always sends weak ETags when enabled.
options.indexOptional. Directory index file. Default 'index.html'. Set false to disable.
options.fallthroughOptional. Default true: missing files call next(). false forwards client errors with next(err).
options.dotfilesOptional. How to treat dotfiles: 'ignore' (default), 'allow', or 'deny'.
options.extensionsOptional. Extension fallbacks when the exact file is missing (for example ['html', 'htm']).
options.setHeadersOptional. fn(res, path, stat) — set custom headers synchronously before the file is sent.

Return Value

express.static(root, [options]) returns middleware. Mount it with app.use — optionally under a URL prefix.

Express
app.use(express.static('public'));
// or
app.use('/static', express.static('public'));

Express.js Examples

Example 1 — Serve Files from public

Mount the folder once; request assets without the folder name in the URL.

Express
const path = require('path');
const express = require('express');
const app = express();

app.use(express.static(path.join(__dirname, 'public')));

app.listen(3000);

How It Works

  1. Pick an absolute root. path.join(__dirname, 'public') stays correct even if you start Node from another directory.
  2. Mount without a prefix. app.use(express.static(…)) maps / onto that folder.
  3. URL = path inside the folder. public/css/style.css → /css/style.css.
  4. No manual route needed. The middleware sends the file when it exists.

Example 2 — Virtual Path Prefix

Serve the same folder under a URL prefix such as /static.

Express
const path = require('path');
const express = require('express');
const app = express();

app.use('/static', express.static(path.join(__dirname, 'public')));

app.listen(3000);

How It Works

  1. Mount path is virtual. /static does not need to exist as a folder name on disk.
  2. Strip the prefix, then look up. /static/js/app.js maps to public/js/app.js.
  3. Useful for clear asset URLs. Keeps API routes at / and assets under /static.
  4. Still no public in the URL. Only the mount prefix you chose appears.

Example 3 — Cache Headers and Multiple Folders

Set a cache lifetime, then fall through to a second static directory if needed.

Express
const path = require('path');
const express = require('express');
const app = express();

app.use(express.static(path.join(__dirname, 'public'), {
  maxAge: '1d',
  etag: true
}));

app.use(express.static(path.join(__dirname, 'files')));

app.listen(3000);

How It Works

  1. Pass options on the first mount. maxAge: '1d' tells browsers they may cache for a day.
  2. Search order matters. Express tries public first, then files.
  3. Default fallthrough helps stacking. A miss in the first folder calls next() so the second can run.
  4. Production tip. For heavy traffic, put a reverse proxy or CDN in front of these assets.

Runtime Support

express.static() is Express.js built-in middleware. It runs on a Node.js server (Express 5 needs Node.js 18+). Browsers only download the files it serves.

✓ Server · Express 4 & 5

express.static()

Works wherever Express runs: Node.js is the primary target. Bun and Deno can run many Express apps with compatibility layers. Not available in the browser.

Server Node.js runtime API
Node.js 18+ for Express 5 · 14+ typical for Express 4
Supported
Bun Express-compatible server apps
Supported
Deno Via npm compatibility / adapters
Supported
express.static() Express on Node.js

Bottom line: Study and run these examples with Node.js and the express package. Create a public folder, copy a snippet, and start the server with node.

Frequently Asked Questions

It is built-in middleware that serves files from a root folder. Express maps the request URL onto that folder and sends the file when it exists.
No. With app.use(express.static('public')), public/css/style.css is served at /css/style.css — not /public/css/style.css.
Mount with a path: app.use('/static', express.static('public')). Then files are available under /static/...
By default (fallthrough: true), the middleware calls next() so later routes or other static folders can handle the request.
No. It runs on your Node.js server. Browsers only request the asset URLs your middleware serves.

Next: express.urlencoded()

Parse URL-encoded form bodies into req.body.

express.urlencoded() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful