Server Port Readiness Checker

0.3.1 · abandoned · verified Tue Apr 21

server-ready is a lightweight Node.js utility designed to ascertain when a network server is listening and ready to accept TCP connections on a specified port. The package operates by repeatedly attempting to establish a TCP connection to the target port, polling every 250 milliseconds until a connection is successful or a default timeout of 20 seconds is reached. While functional, it is important to note that the package, currently at version 0.3.1, has not received updates since 2017, suggesting it is unmaintained. It is primarily intended for use in Node.js scripts, deployment processes, or testing environments where programmatic waiting for server startup is required. Its key differentiator is its simplicity and singular focus on port availability, without deeper application-level health checks.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `server-ready` to wait for a simulated HTTP server to become available on a specific port, then makes a request to confirm.

const serverReady = require('server-ready');
const http = require('http');

const port = 3001;

// Simulate a server that takes a moment to start
const server = http.createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end('Server is ready!\n');
});

// Start the server after a short delay
setTimeout(() => {
  server.listen(port, () => {
    console.log(`Simulated server started on port ${port}`);
  });
}, 1000);

// Use server-ready to detect when the port is open
console.log(`Waiting for server to be ready on port ${port}...`);
serverReady(port, (err) => {
  if (err) {
    console.error(`Error: ${err.message}`);
  } else {
    console.log(`Port ${port} is open or has just opened.`);
    // Make a request to the ready server
    http.get(`http://localhost:${port}`, (res) => {
      let data = '';
      res.on('data', (chunk) => data += chunk);
      res.on('end', () => {
        console.log(`Received from server: ${data.trim()}`);
        server.close(() => console.log('Server closed.'));
      });
    }).on('error', (e) => {
      console.error(`HTTP request error: ${e.message}`);
      server.close(() => console.log('Server closed due to HTTP error.'));
    });
  }
});

// Optional: Set a custom timeout for server-ready
// serverReady.timeout = 5000; // 5 seconds

view raw JSON →