express-port-switcher
Version:
A smart port management tool that automatically finds and forwards to available ports when preferred ports are in use
30 lines (29 loc) • 938 B
TypeScript
import { Express } from 'express';
export interface PortSwitcherOptions {
preferredPort?: number;
maxAttempts?: number;
onPortSwitch?: (preferredPort: number, actualPort: number) => void;
}
export declare class PortSwitcher {
private actualPort;
private preferredPort;
private maxAttempts;
private onPortSwitch?;
private server;
constructor(options?: PortSwitcherOptions);
/**
* Start the Express app on the preferred port, or switch to next available port if occupied
* @param app Express application instance
* @returns Promise resolving to the actual port the server is running on
*/
listen(app: Express): Promise<number>;
/**
* Get the actual port the server is running on
*/
getActualPort(): number | null;
/**
* Close the server
*/
close(): Promise<void>;
}
export { isPortAvailable, findNextAvailablePort } from './lib/portUtils';