res.sendFile()
Streams a file to the client, inferring Content-Type and supporting ranges.
Syntax
res.sendFile(path[, options][, callback]) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
path | string | Yes | The absolute file path (or relative with `options.root`). |
options | object | No | `root`, `maxAge`, `headers`, `dotfiles`, `acceptRanges`. |
callback | function | No | `(err)` invoked when the transfer finishes or fails. |
Returns
void — Streams the file response.
Examples
app.get('/', (req, res) => {
res.sendFile('index.html', { root: './public' });
});
Output
Content-Type: text/html; charset=UTF-8
import { join } from 'node:path';
app.get('/docs/:name', (req, res) => {
res.sendFile(join(import.meta.dirname, 'docs', req.params.name), (err) => {
if (err) res.sendStatus(404);
});
});
Notes
The path must be absolute unless you pass `options.root`. Without
`root`, Express rejects paths containing `..` to block traversal;
still validate filenames from user input. Sets `Content-Type`,
`Last-Modified`, and ETag and supports range requests.