File Serving Guide
Learn how to serve static file from your Invoke functions. There are built-in middlewares to support common scenarios in Bun runtime.
Serve a static directory
Use invoke.serve.static(...) middleware for regular static assets.
import path from 'path'
const publicDir = path.join(process.cwd(), 'public')
export default invoke.serve.static(publicDir, {
maxAge: '1h',
etag: true,
immutable: false,
index: ['index.html']
})
Since invoke.serve.static(...) itself is a middleware, you can also chain it with Router.
import path from 'path'
const router = new Router()
const publicDir = path.join(process.cwd(), 'public')
router.get(
'/static',
invoke.serve.static(publicDir, {
maxAge: '1h',
etag: true,
immutable: false,
index: ['index.html']
})
)
router.post('/api/xxx', (req, res) => {
// Your API here
})
export default router
Useful options:
maxAge,immutable,etagfor caching behaviorindexfor directory index filesdotfiles,extensions,setHeadersfor advanced behaviorfallthroughif you want missing files to pass to next middleware
Serve an SPA
Use invoke.serve.spa(...) middleware for history-based client routing.
import path from 'path'
const distDir = path.join(process.cwd(), 'dist')
export default invoke.serve.spa(distDir, {
index: '/index.html',
rewrites: [{ from: /^\/admin/, to: '/index.html' }]
})
Serve one specific file
Use res.sendFile(...) when you want to return a known file directly.
- JavaScript
- TypeScript
- C#
export default function handler(req, res) {
res.sendFile('/var/task/public/index.html')
}
import { InvokeRequest, InvokeResponse } from 'invoke-bun'
export default function handler(req: InvokeRequest, res: InvokeResponse) {
res.sendFile('/var/task/public/index.html')
}
using Invoke;
[EntryPoint]
public static Task EntryPoint(InvokeRequest req, InvokeResponse res)
{
res.SendFile("/var/task/public/index.html");
return Task.CompletedTask;
}
Force download
Use res.download(...) (or res.Download(...) in C#) to prompt a download.
- JavaScript
- TypeScript
- C#
export default function handler(req, res) {
res.download('/var/task/reports/monthly-report.pdf', 'monthly-report.pdf')
}
import { InvokeRequest, InvokeResponse } from 'invoke-bun'
export default function handler(req: InvokeRequest, res: InvokeResponse) {
res.download('/var/task/reports/monthly-report.pdf', 'monthly-report.pdf')
}
using Invoke;
[EntryPoint]
public static Task EntryPoint(InvokeRequest req, InvokeResponse res)
{
res.Download("/var/task/reports/monthly-report.pdf", "monthly-report.pdf");
return Task.CompletedTask;
}
MIME Types
sendFile / SendFile automatically sets MIME type from file extension.