express, koa-static, and serve-static are all used to serve static files (like HTML, CSS, JS, images) from a Node.js server, but they belong to different ecosystems and serve distinct architectural roles. express is a full-featured web framework that includes routing, middleware, and request handling. serve-static is a dedicated Express-compatible middleware for serving static files. koa-static is the equivalent middleware for the Koa framework, which follows a more minimal and modern async/await-based design. While express can serve static files using serve-static, it is not its primary purpose — similarly, koa-static only works within a Koa application.
When you need to deliver HTML, JavaScript, CSS, images, or other static files from a Node.js backend, your choice of tool depends heavily on your application’s architecture. The packages express, koa-static, and serve-static are often mentioned together, but they solve different problems at different layers. Let’s clarify what each does — and when to use which.
express is a full web application framework. It handles routing, middleware, request parsing, and response formatting. While it can serve static files, it delegates that job to middleware like serve-static.
// express alone cannot serve static files efficiently
// You must use serve-static as middleware
import express from 'express';
const app = express();
// This won't work without serve-static
// app.use(express.static('public')); // actually uses serve-static under the hood
💡 Note:
express.staticis just an alias forserve-static. When you callexpress.static(), you’re using theserve-staticpackage.
serve-static is a standalone middleware designed specifically to serve static files. It works with Express, Connect, or any framework that follows the (req, res, next) middleware signature.
// Using serve-static directly (though usually via express.static)
import serveStatic from 'serve-static';
import finalhandler from 'finalhandler';
import http from 'http';
import fs from 'fs';
const serve = serveStatic('public', { index: ['index.html'] });
const server = http.createServer((req, res) => {
serve(req, res, finalhandler(req, res));
});
koa-static is the Koa-specific equivalent of serve-static. It only works in Koa applications and leverages Koa’s context (ctx) object and async middleware model.
// koa-static in a Koa app
import Koa from 'koa';
import serve from 'koa-static';
const app = new Koa();
app.use(serve('public'));
None of these packages are “static servers” by themselves — except when composed properly. Here’s how each is typically used:
serve-static under the hood)import express from 'express';
const app = express();
// This is the standard way
app.use(express.static('public', {
maxAge: '1d',
etag: true,
fallthrough: false
}));
app.listen(3000);
Under the hood, express.static = require('serve-static'). So you’re really using serve-static.
koa-static)import Koa from 'koa';
import serve from 'koa-static';
const app = new Koa();
app.use(serve('public', {
maxage: 86400000, // 1 day in ms
hidden: false,
index: 'index.html'
}));
app.listen(3000);
serve-static (no framework)import http from 'http';
import serveStatic from 'serve-static';
import finalhandler from 'finalhandler';
import path from 'path';
const serve = serveStatic(path.join(process.cwd(), 'public'), {
index: ['index.html']
});
http.createServer((req, res) => {
serve(req, res, finalhandler(req, res));
}).listen(3000);
🔒 Important:
serve-staticrequires a final handler (likefinalhandler) to manage 404s and errors in standalone mode.
All three approaches support common static-serving needs, but through different APIs.
Express / serve-static:
app.use(express.static('public', {
maxAge: '7d',
etag: true
}));
Koa / koa-static:
app.use(serve('public', {
maxage: 7 * 24 * 60 * 60 * 1000, // 7 days in ms
immutable: true
}));
Note the spelling difference: maxAge (serve-static) vs maxage (koa-static).
serve-static hides dotfiles by default (dotfiles: 'ignore'). You can allow them:
express.static('public', { dotfiles: 'allow' })
koa-static also hides them by default (hidden: false). Enable with:
serve('public', { hidden: true })
Both let you define fallback index files:
// serve-static
express.static('public', { index: ['default.html', 'index.html'] })
// koa-static
serve('public', { index: 'default.html' }) // only accepts string, not array
⚠️ Limitation:
koa-staticonly supports a single index filename, whileserve-staticaccepts an array for fallbacks.
serve-static works with Express, Connect, or any (req, res, next) middleware system. It does not work with Koa.koa-static only works with Koa. It will throw errors if used in an Express app.express is a complete framework — you don’t “add” it to Koa or vice versa.Trying to use koa-static in Express:
// ❌ This fails
import express from 'express';
import koaStatic from 'koa-static';
const app = express();
app.use(koaStatic('public')); // TypeError: ctx is not defined
Similarly, serve-static won’t work in Koa without adaptation:
// ❌ This also fails
import Koa from 'koa';
import serveStatic from 'serve-static';
const app = new Koa();
app.use(serveStatic('public')); // Doesn't match Koa's async middleware signature
You have an Express backend serving JSON APIs and also need to deliver a React SPA.
express + express.static() (i.e., serve-static)app.use('/api/users', userRoutes);
app.use(express.static('build')); // serves index.html for SPA
You’re using Koa for its async simplicity and need to serve Swagger UI or docs.
koa-staticapp.use(serve('docs'));
You just want to serve a folder over HTTP with caching and gzip.
serve-static directly with Node’s http moduleserve-static is battle-tested for this exact use case.Myth: “express serves static files natively.”
serve-static via express.static.Myth: “koa-static is a drop-in replacement for serve-static.”
Myth: “I can use serve-static in Koa with a wrapper.”
koa-static instead for cleaner code.| Package | Type | Framework Required | Input Shape | Key Strength |
|---|---|---|---|---|
express | Full framework | None (it is the framework) | N/A | End-to-end app development |
serve-static | Middleware | Express/Connect | (req, res, next) | Production-grade static serving |
koa-static | Middleware | Koa | ctx, next | Clean integration with Koa apps |
express.static() — which is serve-static.koa-static.express just to serve static files — that’s overkill. Use serve-static standalone or with a minimal server.The right choice isn’t about which package is “better” — it’s about matching the tool to your application’s foundation. Get that right, and static asset delivery becomes a solved problem.
Choose express if you're building a full web application or API that requires routing, middleware composition, error handling, and other server-side features beyond just serving static files. It’s ideal when you need an all-in-one solution with a mature ecosystem and extensive third-party middleware support. Don’t use it solely for static file serving — pair it with serve-static for that task.
Choose koa-static only if you’re already using the Koa framework and need to serve static assets. It integrates cleanly with Koa’s middleware pipeline and async/await pattern. Avoid it if you’re not using Koa — it won’t work with Express or standalone servers.
Choose serve-static when you need a robust, production-ready static file server that integrates with Express (or any Connect-compatible framework). It offers fine-grained control over caching, directory listing, and file resolution. Use it as middleware within an Express app — not as a standalone server.
Fast, unopinionated, minimalist web framework for Node.js.
This project has a Code of Conduct.
import express from 'express'
const app = express()
app.get('/', (req, res) => {
res.send('Hello World')
})
app.listen(3000, () => {
console.log('Server is running on http://localhost:3000')
})
This is a Node.js module available through the npm registry.
Before installing, download and install Node.js. Node.js 18 or higher is required.
If this is a brand new project, make sure to create a package.json first with
the npm init command.
Installation is done using the
npm install command:
npm install express
Follow our installing guide for more information.
PROTIP Be sure to read the migration guide to v5
The quickest way to get started with express is to utilize the executable express(1) to generate an application as shown below:
Install the executable. The executable's major version will match Express's:
npm install -g express-generator@4
Create the app:
express /tmp/foo && cd /tmp/foo
Install dependencies:
npm install
Start the server:
npm start
View the website at: http://localhost:3000
The Express philosophy is to provide small, robust tooling for HTTP servers, making it a great solution for single page applications, websites, hybrids, or public HTTP APIs.
Express does not force you to use any specific ORM or template engine. With support for over 14 template engines via @ladjs/consolidate, you can quickly craft your perfect framework.
To view the examples, clone the Express repository:
git clone https://github.com/expressjs/express.git --depth 1 && cd express
Then install the dependencies:
npm install
Then run whichever example you want:
node examples/content-negotiation
The Express.js project welcomes all constructive contributions. Contributions take many forms, from code for bug fixes and enhancements, to additions and fixes to documentation, additional tests, triaging incoming pull requests and issues, and more!
See the Contributing Guide for more technical details on contributing.
If you discover a security vulnerability in Express, please see Security Policies and Procedures.
To run the test suite, first install the dependencies:
npm install
Then run npm test:
npm test
For information about the governance of the express.js project, see GOVERNANCE.md.
The original author of Express is TJ Holowaychuk