Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check if port is busy in NodeJS?

How can I check if port is busy for localhost?

Is there any standard algorithm? I am thinking at making a http request to that url and check if response status code is not 404.

like image 827
Ionică Bizău Avatar asked Oct 02 '13 03:10

Ionică Bizău


People also ask

How do I know if a port is open in node JS?

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.

How can I tell if a port is occupied?

You can use "netstat" to check whether a port is available or not. Use the netstat -anp | find "port number" command to find whether a port is occupied by an another process or not. If it is occupied by an another process, it will show the process id of that process.

How do I check NodeJS server status?

To check the node server running by logging in to the system In windows you can simply go to the Task Manager and check for node in the application list. If it is there then it is running in the machine.

Why does node debug hang when port is open?

Then in the parent, a connection is attempted to that port. However if you have another "node debug" session running, currently "node debug" hangs because that port is in use.

How do I know if a port is open?

If you see a message that says "Could not open connection," the port is not open. Open a Terminal window. To do this, open Spotlight by clicking the magnifying glass at the top-right corner of the screen, type terminal, and then click Terminal in the search results. Use this method to see if a port is open on your local router or access point.

Why does node return false when port is taken on IPv6?

These aren't exactly the same thing, since newer versions of Node try IPv6 and then fall back to IPv4, so it returns false even though a process is listening, since a second process can still listen on the other IP version. The following code returns true if the port is taken on either IPv4 or IPv6:

How to find in use ports on a host?

There's an easy solution to this problem: Node Port Scanner. This utility provides methods for finding in use or available ports on a given host! The most common use case to solve port collisions would be findAPortNotInUse:


Video Answer


2 Answers

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.

like image 155
hexacyanide Avatar answered Oct 20 '22 01:10

hexacyanide


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.

like image 29
Codebling Avatar answered Oct 20 '22 02:10

Codebling