CLI Commands
The bxn CLI provides a simple command-line interface for running your API in development and production modes.
Commands
Section titled “Commands”Start the server in production or development mode.
bxn start [options]Features:
- Auto-detects routes directory (
src/routesorlib/routes) - Runs compiled JavaScript or TypeScript source files
- Optional watch mode using Node.js native
--watchflag
Options:
| Option | Alias | Type | Default | Description |
|---|---|---|---|---|
--port | -p | number | 3000 | Port to listen on |
--routes | - | string | ./src/routes with --watch./lib/routes without --watch | Routes directory path |
--watch | - | boolean | false | Enable watch mode with Node.js —watch |
--key | - | string | - | Path to SSL private key file (for HTTPS) |
--cert | - | string | - | Path to SSL certificate file (for HTTPS) |
Examples:
# Basic usage (auto-detects routes)bxn start
# Development mode with hot reloadbxn start --watch
# Custom portbxn start --port 8080bxn start -p 8080
# Development with watch mode and custom portbxn start --watch --port 8080
# Custom routes directorybxn start --routes ./dist/routes
# HTTPS in productionbxn start --port 443 --key ./ssl/key.pem --cert ./ssl/cert.pem
# HTTPS in development with watch modebxn start --watch --key ./ssl/key.pem --cert ./ssl/cert.pemHTTPS Support
Section titled “HTTPS Support”The bxn start command supports HTTPS by providing SSL certificate files.
Requirements
Section titled “Requirements”To enable HTTPS, you must provide both:
--key- Path to your SSL private key file (PEM format)--cert- Path to your SSL certificate file (PEM format)
Both options must be provided together. If only one is provided, the server will start in HTTP mode.
Development Setup
Section titled “Development Setup”For local development, you can generate a self-signed certificate:
# Generate self-signed certificate (valid for 365 days)openssl req -x509 -newkey rsa:4096 \ -keyout key.pem -out cert.pem \ -days 365 -nodes \ -subj "/C=US/ST=State/L=City/O=Organization/CN=localhost"
# Start server with HTTPS and watch modebxn start --watch --key ./key.pem --cert ./cert.pemYour server will be available at https://localhost:3000
Note: Browsers will show a security warning for self-signed certificates. This is expected for development.
Production Setup
Section titled “Production Setup”For production, use certificates from a trusted Certificate Authority (CA):
# Using Let's Encrypt certificates (example paths)bxn start \ --port 443 \ --key /etc/letsencrypt/live/yourdomain.com/privkey.pem \ --cert /etc/letsencrypt/live/yourdomain.com/fullchain.pemExamples
Section titled “Examples”# Development with HTTPS and watch modebxn start --watch --key ./ssl/key.pem --cert ./ssl/cert.pem
# Development with HTTPS on custom portbxn start --watch --port 3443 --key ./ssl/key.pem --cert ./ssl/cert.pem
# Production with HTTPSbxn start --port 443 --key ./ssl/key.pem --cert ./ssl/cert.pem
# Production with HTTPS and custom routesbxn start \ --port 443 \ --routes ./dist/routes \ --key ./ssl/key.pem \ --cert ./ssl/cert.pemPath Resolution
Section titled “Path Resolution”Certificate file paths are resolved relative to the current working directory:
# Relative pathsbxn start --key ./ssl/key.pem --cert ./ssl/cert.pem
# Absolute pathsbxn start --key /etc/ssl/key.pem --cert /etc/ssl/cert.pemProgrammatic HTTPS
Section titled “Programmatic HTTPS”You can also configure HTTPS programmatically using the createServer API:
import { createServer } from '@buildxn/http';import { readFileSync } from 'fs';
const server = createServer(routes, { https: { key: readFileSync('./ssl/key.pem'), cert: readFileSync('./ssl/cert.pem'), },});
server.listen(443);Watch Mode
Section titled “Watch Mode”The --watch flag uses Node.js native --watch feature for automatic server restarts when files change.
How it works:
- When you run
bxn start --watch, the CLI automatically re-spawns itself withnode --watch - Node.js watches all imported modules and restarts the server when they change
- No additional configuration or patterns needed
Example:
# Development with auto-reloadbxn start --watch
# With custom portbxn start --watch --port 8080
# With HTTPSbxn start --watch --key ./ssl/key.pem --cert ./ssl/cert.pemEnvironment Variables
Section titled “Environment Variables”Set the default port via environment variable:
PORT=8080 bxn startPORT=8080 bxn start --watchNote: The --port flag takes precedence over the PORT environment variable.
Common Workflows
Section titled “Common Workflows”Development
Section titled “Development”# Start development server with watch modepnpm dev# ornpx bxn start --watchProduction
Section titled “Production”# 1. Build your TypeScript codepnpm build# ortsc
# 2. Start production serverpnpm start# ornpx bxn startCustom Setup
Section titled “Custom Setup”# Development with custom configurationbxn start --watch --port 8080 --routes ./src/api/routes
# Production with custom configurationbxn start --port 8080 --routes ./dist/api/routesConfiguration via package.json
Section titled “Configuration via package.json”Add scripts to your package.json for convenience:
{ "scripts": { "dev": "bxn start --watch", "start": "bxn start", "build": "tsc", "prod": "npm run build && bxn start" }}Then run:
npm run devnpm run startnpm run prodRoutes Directory Selection
Section titled “Routes Directory Selection”The bxn start command automatically selects the routes directory based on the --watch flag:
- With
--watch: Uses./src/routes(development mode with TypeScript source files) - Without
--watch: Uses./lib/routes(production mode with compiled JavaScript) - With
--routes: Uses the specified path (overrides the default behavior)
This creates a clear contract:
# Development mode: uses src/routesbxn start --watch
# Production mode: uses lib/routes (after building)npm run buildbxn start
# Custom routes directory (overrides default)bxn start --routes ./custom/routesbxn start --watch --routes ./custom/routesWhy this design?
- Predictable: You always know which directory is used based on the flag
- Simple: No filesystem checks or guessing
- Best practices: Enforces the natural workflow (develop in
src/, run production fromlib/)
Examples
Section titled “Examples”Development with Custom Port
Section titled “Development with Custom Port”bxn start --watch --port 8080Your API will be available at http://localhost:8080
Production with Custom Routes
Section titled “Production with Custom Routes”# After buildingtsc
# Run with custom routes directorybxn start --routes ./dist/api/routesTroubleshooting
Section titled “Troubleshooting”Port Already in Use
Section titled “Port Already in Use”If you get a port conflict error:
# Use a different portbxn start --port 3001Or set the PORT environment variable:
PORT=3001 bxn startRoutes Not Loading
Section titled “Routes Not Loading”Verify your routes directory:
# Check if src/routes exists (development)ls -la ./src/routes
# Check if lib/routes exists (production)ls -la ./lib/routes
# Or specify custom locationbxn start --routes ./your/routes/pathWatch Mode Not Working
Section titled “Watch Mode Not Working”Make sure you’re using the --watch flag:
# Correct - enables watch modebxn start --watch
# Without --watch, changes won't trigger restartsbxn startNext Steps
Section titled “Next Steps”- Read about File-System Routing to understand route structure
- See Quick Start for a complete setup guide