How can I check if port is busy in NodeJS?

后端 未结 2 510
我在风中等你
我在风中等你 2021-01-04 06:19

How can I check if port is busy for localhost?

Is there any standard algorithm? I am thinking at making a http request to

相关标签:
2条回答
  • 2021-01-04 06:31

    Check out the amazing tcp-port-used node module!

    //Check if a port is open
    tcpPortUsed.check(port [, host]) 
    
     //Wait until a port is no longer being used
    tcpPortUsed.waitUntilFree(port [, retryTimeMs] [, timeOutMs])
    
    //Wait until a port is accepting connections
    tcpPortUsed.waitUntilUsed(port [, retryTimeMs] [, timeOutMs])
    
    //and a few others!
    

    I've used these to great effect with my gulp watch tasks for detecting when my Express server has been safely terminated and when it has spun up again.

    This will accurately report whether a port is bound or not (regardless of SO_REUSEADDR and SO_REUSEPORT, as mentioned by @StevenVachon).

    The portscanner NPM module will find free and used ports for you within ranges and is more useful if you're trying to find an open port to bind.

    0 讨论(0)
  • 2021-01-04 06:42

    You could attempt to start a server, either TCP or HTTP, it doesn't matter. Then you could try to start listening on a port, and if it fails, check if the error code is EADDRINUSE.

    var net = require('net');
    var server = net.createServer();
    
    server.once('error', function(err) {
      if (err.code === 'EADDRINUSE') {
        // port is currently in use
      }
    });
    
    server.once('listening', function() {
      // close the server if listening doesn't fail
      server.close();
    });
    
    server.listen(/* put the port to check here */);
    

    With the single-use event handlers, you could wrap this into an asynchronous check function.

    0 讨论(0)
提交回复
热议问题