BunPress Documentation
⌘ K

Get Started

There are two ways of using this reverse proxy: as a library or as a CLI.

Library

Given the npm package is installed:

import type { TlsConfig } from'@stacksjs/bunpress' '@stacksjs/bunpress'
import { startProxy } from'@stacksjs/bunpress' '@stacksjs/bunpress'

export interface CleanupConfig {
  hosts: boolean // clean up /etc/hosts, defaults to false
  certs: boolean // clean up certificates, defaults to false
}

export interface ReverseProxyConfig {
  from: string // domain to proxy from, defaults to localhost:3000
  to: string // domain to proxy to, defaults to stacks.localhost
  cleanUrls?: boolean // removes the .html extension from URLs, defaults to false
  https: boolean | TlsConfig // automatically uses https, defaults to true, also redirects http to https
  cleanup?: boolean | CleanupConfig // automatically cleans up /etc/hosts, defaults to false
  verbose: boolean // log verbose output, defaults to false
}

const config: ReverseProxyOptions = {
  from:'localhost:3000' 'localhost:3000',
  to:'my-docs.localhost' 'my-docs.localhost',
  cleanUrls: true,
  https: true,
  cleanup: false,
}

startProxy(config)

In case you are trying to start multiple proxies, you may use this configuration:

// reverse-proxy.config.{ts,js}
import type { ReverseProxyOptions } from'@stacksjs/bunpress' '@stacksjs/bunpress'
import os from'node:os' 'node:os'
import path from'node:path' 'node:path'

const config: ReverseProxyOptions = {
  https: { // https: true -> also works with sensible defaults
    caCertPath: path.join(os.homedir(),'.stacks' '.stacks','ssl' 'ssl', `stacks.localhost.ca.crt`),
    certPath: path.join(os.homedir(),'.stacks' '.stacks','ssl' 'ssl', `stacks.localhost.crt`),
    keyPath: path.join(os.homedir(),'.stacks' '.stacks','ssl' 'ssl', `stacks.localhost.crt.key`),
  },

  cleanup: {
    hosts: true,
    certs: false,
  },

  proxies: [
    {
      from:'localhost:5173' 'localhost:5173',
      to:'my-app.localhost' 'my-app.localhost',
      cleanUrls: true,
    },
    {
      from:'localhost:5174' 'localhost:5174',
      to:'my-api.local' 'my-api.local',
    },
  ],

  verbose: true,
}

export default config

CLI

bunpress --from localhost:3000 --to my-project.localhost
bunpress --from localhost:8080 --to my-project.test --keyPath ./key.pem --certPath ./cert.pem
bunpress --help
bunpress --version

Testing

bun test