Skip to main content

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, etag for caching behavior
  • index for directory index files
  • dotfiles, extensions, setHeaders for advanced behavior
  • fallthrough if 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.

export default function handler(req, res) {
res.sendFile('/var/task/public/index.html')
}

Force download

Use res.download(...) (or res.Download(...) in C#) to prompt a download.

export default function handler(req, res) {
res.download('/var/task/reports/monthly-report.pdf', 'monthly-report.pdf')
}

MIME Types

sendFile / SendFile automatically sets MIME type from file extension.

Next Steps