Recipes
The samples serve the folder public/ next to the sample file; Serve Several Folders adds legacy/.
Serve Several Folders
Register the plugin once per folder. A request without a file in the first folder goes on to the second.
import { fileURLToPath } from 'node:url';
import { App } from 'routup';
import { assets } from '@routup/assets';
export const app = new App();
// checked first: a file here wins over one of the same name below
app.use(assets(fileURLToPath(new URL('public', import.meta.url))));
// reached when public/ has no matching file
app.use(assets(fileURLToPath(new URL('legacy', import.meta.url))));curl http://localhost:3000/about
# <!doctype html> ... <h1>About</h1>
curl http://localhost:3000/old-page
# <!doctype html> ... <h1>Old Page</h1>Serve Under a Path Prefix
With a path, the plugin serves only requests under it and looks up the rest of the path in the folder. Requests outside the prefix reach the other routes.
import { fileURLToPath } from 'node:url';
import { App, defineCoreHandler } from 'routup';
import { assets } from '@routup/assets';
export const app = new App();
// GET /static/about serves public/about.html
app.use('/static', assets(fileURLToPath(new URL('public', import.meta.url))));
app.get('/about', defineCoreHandler(() => ({ page: 'about' })));curl http://localhost:3000/static/about
# <!doctype html> ... <h1>About</h1>
curl http://localhost:3000/about
# {"page":"about"}Cache Hashed Files
Files with a content hash in the name can be cached for a year; the other files are revalidated with their ETag on every use.
import { fileURLToPath } from 'node:url';
import { App } from 'routup';
import { assets } from '@routup/assets';
export const app = new App();
// content-hashed file names never change: cache them for a year
app.use('/assets', assets(fileURLToPath(new URL('public/assets', import.meta.url)), {
cacheMaxAge: 31536000,
cacheImmutable: true,
}));
// everything else: browsers revalidate with the ETag on every use
app.use(assets(fileURLToPath(new URL('public', import.meta.url))));curl -I http://localhost:3000/assets/app.3f9a1c.css
# cache-control: public,max-age=31536000,immutable
curl -I http://localhost:3000/about
# cache-control: public,max-age=0,must-revalidateAnswer Missing Files
resolve builds the response for a path without a file, here a JSON 404 instead of passing the request on.
import { fileURLToPath } from 'node:url';
import { App } from 'routup';
import { assets } from '@routup/assets';
export const app = new App();
app.use('/files', assets(fileURLToPath(new URL('public', import.meta.url)), {
resolve: async (event) => {
event.response.status = 404;
return { message: `${event.path} not found` };
},
}));curl -i http://localhost:3000/files/nope.txt
# HTTP/1.1 404 Not Found
# {"message":"/files/nope.txt not found"}Single-Page App
A single-page app gets its index.html for every path its client-side router handles, while missing assets and API paths do not: fallback with fallbackIgnores.
import { fileURLToPath } from 'node:url';
import { App, defineCoreHandler } from 'routup';
import { assets } from '@routup/assets';
export const app = new App();
app.use(assets(fileURLToPath(new URL('public', import.meta.url)), {
// index.html for the paths of the client-side router
fallback: true,
// not for API paths; a missing file with an extension is left out too
fallbackIgnores: ['^/api(/|$)'],
}));
app.get('/api/todos', defineCoreHandler(() => [{ title: 'Write docs' }]));curl http://localhost:3000/todos/42
# <!doctype html> ... <h1>Home</h1>
curl http://localhost:3000/api/todos
# [{"title":"Write docs"}]
curl -i http://localhost:3000/assets/missing.js
# HTTP/1.1 404 Not FoundThe Static Files example on routup.dev shows a larger setup.