Wait for localhost to be ready
Useful if you need a local server to be ready to accept requests before doing other things.
Supports both HTTP/1 and HTTP/2 servers with automatic fallback.
npm install wait-for-localhost
import waitForLocalhost from 'wait-for-localhost';
await waitForLocalhost({port: 8080});
console.log('Server is ready');
Returns a Promise<object>
that settles when localhost is ready.
The object contains a ipVersion
property with a value of either 6
or 4
depending on the IP version that was used.
Type: object
Type: number
Default: 80
Type: string
Default: '/'
Use a custom path.
For example, /health
for a health-check endpoint.
Type: boolean
Default: false
Use the GET
HTTP-method instead of HEAD
to check if the server is running.
Type: number[]
Default: [200]
HTTP status codes to consider as successful responses.
Type: AbortSignal
An AbortSignal to abort the operation.
import waitForLocalhost from 'wait-for-localhost';
// Timeout after 5 seconds
await waitForLocalhost({
port: 8080,
signal: AbortSignal.timeout(5000)
});
- wait-for-localhost-cli - CLI for this module
- delay - Delay execution for a given amount of seconds