res.sendFile() transfers a file from disk as the HTTP response body. Express sets Content-Type from the filename extension. Unless you set root, path must be absolute.
Use it when a route should return a specific file (PDF, image, HTML page). For a forced Save dialog, prefer res.download(). For many public assets, static middleware is usually simpler than one route per file.
Syntax
Call res.sendFile in your Express.js app. Start with this line.
Required. File to transfer. Absolute unless options.root is set.
options
Optional. Serving options (root, maxAge, headers, and more).
fn
Required. fn(err) on complete or error. On error you must finish the response or call next.
root
Required. Absolute directory; relative path must resolve inside it.
maxAge
Required. Cache-Control max-age (ms number or ms string like '1h').
lastModified
Required. Set false to omit the Last-Modified header.
headers
Required. Extra response headers object.
dotfiles
Required. "allow" | "deny" | "ignore".
acceptRanges
Required. Enable or disable ranged requests.
cacheControl
Required. Enable or disable the Cache-Control header.
immutable
Required. Add immutable to Cache-Control; also set maxAge.
Return Value
res.sendFile() transfers a file from disk and finishes the response (unless you pass an error callback you must handle). Keep paths absolute or use root.
GET /terms (file exists)
→ Content-Type: text/plain
→ Body: (terms.txt)
→ console: Terms sent
GET /terms (file missing)
→ err in callback, headers not sent
→ 404 File not found
How It Works
Absolute path again. No root here — the full path must be absolute.
Callback receives err. Missing files typically surface as an error (for example ENOENT).
You must respond. Docs: if a callback is provided and an error occurs, you finish the cycle yourself.
Check headersSent. Mid-stream failures may have already flushed headers — then avoid a second body.
Compatibility
Runtime Support
res.sendFile() is an Express.js response method. It runs on a Node.js server (Express 5 needs Node.js 18+) and reads the local filesystem. Browsers only receive the file bytes.
✓ Server · Express 4.8+ & 5
res.sendFile()
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.
ServerNode.js runtime API
Node.js18+ for Express 5 · Express 4.8+ for sendFile
Supported
BunExpress-compatible server apps
Supported
DenoVia npm compatibility / adapters
Supported
res.sendFile()Express on Node.js
Bottom line: Study and run these examples with Node.js and the express package. Place sample files on disk before testing.
Frequently Asked Questions
It transfers a file from the server filesystem as the response body and sets Content-Type from the filename extension.
Yes, unless you set the root option. With root, path may be relative (even with ..); Express validates it stays inside that directory.
Both stream a disk file. res.download() also sets Content-Disposition to attachment so browsers usually prompt to save. sendFile often opens inline.
End the request-response cycle yourself or pass control with next(err). If you supply a callback, Express does not auto-finish the error for you.
No. That was the Express 3 name. Use camelCase res.sendFile() in Express 4 and 5.