Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Express.js · Module

res.sendFile

res.sendFile() transfers a file from the server filesystem to the client. It accepts a path, optional serving options, and an optional callback for completion or errors.

How it works

Use res.sendFile() to transfer one file to the client. Supply an absolute path, or provide root so the path can be relative; options configure file-serving behavior, and an optional callback handles completion or errors.

Syntax

res.sendFile(path, [options], [fn])

Parameters

NameTypeDescription
pathStringPath to the file to transfer; it must be absolute unless options.root is set.
optionsObject | undefinedOptional file-serving settings, including maxAge, root, lastModified, and headers.
fnFunctionOptional callback invoked after completion or on error; receives err.

Returns

The documentation describes the transfer and callback behavior but does not specify a separate return value.

Key points

  • The method sends one filesystem file as the response.
  • Express derives the response media type from the file extension.
  • The optional options object customizes file delivery.
  • By default, the file's operating-system modification time is exposed through Last-Modified.
  • Use options.headers to add or override headers for the file response.
  • The callback receives an error value when transfer fails and is also called after completion.

Common mistakes

  • Without root, path must be absolute.
  • Treat user-derived absolute paths as a security boundary and construct them securely.
  • When using the callback, end the response or pass the error to the next route.
  • Relative paths may contain .. with root, so choose a trusted root and understand the resulting resolution.

Example: Serve a file from a constrained directory

import express from "express";
import path from "node:path";

const app = express();
const publicDir = path.join(process.cwd(), "public");

app.get("/files/:name", (req, res, next) => {
  res.sendFile(req.params.name, { root: publicDir }, (err) => {
    if (err) next(err);
  });
});

app.listen(3000);

Serves the requested file relative to a fixed directory and forwards transfer errors to Express.

When to use it

Serve a specific file

A route needs to return one known HTML, image, PDF, or other filesystem file.

Serve files under a fixed root

A request selects a filename within a controlled directory.

Customize file response metadata

A response needs custom caching or HTTP headers for one file.

Interview questions

QWhat does res.sendFile() do, and what path form does it require?

It transfers the file at the supplied path and sets Content-Type from the filename extension. The path must be absolute unless options.root is supplied.

Follow-up: How would you safely serve a user-selected file? Use a fixed absolute root and pass a relative name, then handle callback errors with next(err).

QWhich important options does the documentation describe?

The options object configures serving, including maxAge, root, lastModified, and headers. maxAge defaults to 0, while lastModified defaults to true.

Follow-up: What cache-related behavior can be changed? Set maxAge for Cache-Control max-age and set lastModified to false to suppress Last-Modified.

QHow does the callback affect error handling?

The optional fn(err) callback runs after transfer completion or when an error occurs. If an error occurs while using it, the callback must finish the response or delegate to the next route.

Follow-up: What should a typical Express handler do on failure? Call next(err), unless it deliberately sends or ends an error response.

  • res.download()
  • res.send()
  • express.static()
  • res.render()

Source: Express 4.x API — Response. Checked 2026-10-15.