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
.
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.
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.
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.
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.
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.
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:
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:
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With