Class Overview
Constructor
string
required
Path to the state directory where route data will be stored. The directory will be created if it doesn’t exist.
object
Optional configuration
Properties
string
The state directory path (read-only)
string
Path to the proxy PID file (read-only)
string
Path to the proxy port file (read-only)
Methods
ensureDir()
addRoute() and removeRoute().
Example:
addRoute()
string
required
The hostname to register (e.g., “api.localhost”)
number
required
The local port the application is listening on
number
required
Process ID of the owning process. Use
process.pid for the current process, or 0 for system-managed routes.boolean
default:"false"
Override an existing route even if owned by a live process
RouteConflictError- When hostname is already registered by a live process andforceis falseError- When the file lock cannot be acquired
removeRoute()
string
required
The hostname to unregister
Error- When the file lock cannot be acquired
loadRoutes()
boolean
default:"false"
When true, writes the cleaned-up route list back to disk. Only safe when the caller already holds the lock (used internally by
addRoute and removeRoute).RouteMapping[]- Array of live route mappings
getRoutesPath()
File Locking
TheRouteStore uses directory-based file locking to coordinate concurrent access:
- Lock acquisition retries up to 20 times with 50ms delays
- Stale locks (older than 10 seconds) are automatically removed
- All write operations (
addRoute,removeRoute) acquire the lock automatically
Stale Route Cleanup
Routes are automatically cleaned up when:- The owning process terminates (detected via PID check)
- Any method loads the routes from disk
- Routes with
pid: 0are never removed (system-managed)
Error Handling
RouteConflictError
Complete Example
Type Definitions
Constants
Related
createProxyServer
Create a proxy server that uses routes
Type Definitions
Complete type reference