Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/binary-person/rammerhead/llms.txt

Use this file to discover all available pages before exploring further.

addStaticFilesToProxy walks a directory recursively and registers each file as a GET route on the proxy. An index.html at any directory level is additionally mounted at the directory path itself (e.g. /index.html is also served at /). MIME types are detected automatically from file extensions. This is how Rammerhead’s default public/ directory is served. You can call it with your own directory to replace or supplement the built-in UI.

Signature

addStaticFilesToProxy(proxy, staticDir, rootPath?, shouldIgnoreFile?)
proxy
Proxy
required
A RammerheadProxy (or any testcafe-hammerhead Proxy) instance. Files are registered by calling proxy.GET(route, staticContent).
staticDir
string
required
Absolute path to the directory whose contents should be served. Must be a directory — a TypeError is thrown otherwise.
rootPath
string
default:"'/'"
The URL prefix under which files are mounted. Defaults to '/'. Leading and trailing slashes are normalised automatically, so 'ui', '/ui', and '/ui/' are all equivalent.
shouldIgnoreFile
(file: string, dir: string) => boolean
default:"() => false"
Optional predicate called for each file before it is registered. Return true to skip a file. The first argument is the file name; the second is the absolute path of the containing directory.

Return value

undefined. Routes are registered as a side effect on proxy.

Behaviour

  • Recursive: subdirectories are traversed automatically. A file at staticDir/js/app.js with rootPath='/' is served at /js/app.js.
  • index.html shortcut: when a file named index.html is encountered, it is also registered at the parent directory path (e.g. /subdir/), mirroring standard web server behaviour.
  • MIME detection: content types are set via the mime package based on the file extension.
  • Forbidden routes: the following paths are reserved by hammerhead and rammerhead. Mounting a file that would clash with one of them throws a TypeError:
/rammerhead.js
/hammerhead.js
/task.js
/iframe-task.js
/messaging
/transport-worker.js
/worker-hammerhead.js

Example

const addStaticFilesToProxy = require('./src/util/addStaticDirToProxy');
const path = require('path');

// Serve everything in ./public at /
addStaticFilesToProxy(proxyServer, path.join(__dirname, 'public'));

// Serve a separate admin UI at /admin/
addStaticFilesToProxy(proxyServer, path.join(__dirname, 'admin-ui'), '/admin/');

// Serve files but skip source maps
addStaticFilesToProxy(
    proxyServer,
    path.join(__dirname, 'public'),
    '/',
    (file) => file.endsWith('.map')
);

Config integration

In config.js, the publicDir option points to the directory that Rammerhead serves by default:
publicDir: path.join(__dirname, '../public'), // set to null to disable
When the server starts, it calls addStaticFilesToProxy(proxy, config.publicDir) to mount that directory at /. Set publicDir to null to disable static file serving entirely.
All files in staticDir are read into memory at startup and cached as static content. Avoid pointing this at large directories or directories containing sensitive files.

Build docs developers (and LLMs) love