Serving Static Files
Serve files from a local directory using app.serveStatic. This is typically the very next thing you'll register after addDomain — it powers your frontend, assets, and any other files the browser needs. Registers both GET and HEAD handlers automatically:
// Serve everything in ../public at the root
nukebase.app.serveStatic("/*", path.join(__dirname, "../public"));
// Serve a private directory (no auth callback = open access)
nukebase.app.serveStatic("/assets/*", path.join(__dirname, "assets"));
serveStatic auth callback receives (res, req) — res first, then req. This matches the underlying uWebSockets convention and is the opposite of Express.
Signature
app.serveStatic(routePattern, rootDir, auth?)
routePattern- URL pattern with trailing/*(e.g.,"/*","/admin/*"). The matched portion before/*is treated as the mount point and stripped before resolving againstrootDir.rootDir- Absolute path to the directory to serve from. Usepath.join(__dirname, "...").auth(optional) - Async callback returning a boolean. If provided, runs before any file is served. Returntrueto allow,falseto respond with 401.
Auth Callback
The auth callback receives (res, req) — same convention as postWithBody:
// Only logged-in users can access /private/*
nukebase.app.serveStatic("/private/*", path.join(__dirname, "../private"),
async (res, req) => {
return Boolean(req.uid);
}
);
// Admins only
nukebase.app.serveStatic("/admin/*", path.join(__dirname, "../admin"),
async (res, req) => {
return req.claims?.role === "admin";
}
);
The req object passed to the auth callback contains the auth fields populated by checkAuth (uid, username, email, claims, cookies, urlParams, referer, userAgent, ip, url) plus host and method. Identity fields are present only when the user is authenticated. Returning a falsy value sends a 401 response automatically.
The callback is a hook, not just a gate. It runs on every request before a byte is served and receives res, so it is the natural place for cross-cutting work — request logging, page-view analytics, setting a cookie. Do the work, then return true to serve the file normally. Keep it cheap and never let it throw: it sits on the path of every asset your site serves.
Behavior Details
- Index files: Requests ending in
/automatically serveindex.htmlfrom that directory. - Path traversal protection: Any URL that resolves outside
rootDir(e.g., via../) returns 403 Forbidden. - Directories: A URL resolving to a directory without a trailing slash gets a
301 Moved Permanentlyto the same URL with one added, so relative links inside the served page resolve correctly. - Missing files: Return 404 Not Found.
- Streaming: Files are streamed in 16KB chunks rather than buffered into memory — safe for large files.
- Content-Type: Set automatically from the file extension via the
mimepackage; falls back toapplication/octet-streamfor unknown types. - Image caching: Files with
image/*content types receiveCache-Control: public, max-age=31536000(1 year). Other file types are served without cache headers.