Node js port is already in use - node.js

When I start my webserver, node throws listen EADDRINUSE: address already in use :::3000, I tried to use netstat and search for 3000 port (or another, it happens with any port), but nothing found. Also no node processes in task manager, no webpage on localhost:3000. Also I tried to reload windows, but nothing changed.

From your code you are trying to bind the socket module to port 3000:
var io = require('socket.io')(3000);
/* ... */
app.listen(3000);
in this way when the server tries to bind itself the port is already in use.
You have to create the HTTP server and then bind the socket.io module on it:
const app = require('express')();
const server = require('http').createServer(app);
const io = require('socket.io')(server);
io.on('connection', () => { /* … */ });
server.listen(3000);
(taken from the socket.io documentation)

Assuming you are on Windows 10, I would recommend trying Resource Monitor. The Network tab has a view called Listening Ports, which should tell you which process is using port 3000
Resource Monitor

It is because of the port is busy to other tasks,You have to stop them with following commands first command shows you process id that is running on port you can stop this process with second command.
$ sudo netstat -nltp | grep
$ sudo kill

Related

how to force node.js pm2 to run based on ipv4

currently my hosting uses a new firewall, and according to their plan, they don't allow any http connection based on ipv6 and all connections should use ipv4.
I have a service using node.js and expressJs, and I also use pm2 as a process manager to run my application, my problem is that http requests failed due to using ipv6. How could I force node.js to listen to version 4 IP address on nodeJs app.
The part of my code which I listen to a port:
const app = express();
...MANY MIDDLEWARE app.use();
mongoose.connect(MONGODB_URI, {useNewUrlParser: true, useUnifiedTopology: true})
.then(result => {
app.listen(APP_PORT);
socketServer.listen(SOCKET_PORT, function () {
console.log('server listening to: %j', socketServer.address())
});
})
.catch(err => {
console.log
});
Can I use something like below with express:
var server = http.createServer(app).listen(APP_PORT, APP_IP);
I was curious about it too, but based on a useful link it worked like a charm :
https://github.com/nodejs/node/issues/18041.
I had this block of code :
var server = http.createServer(app);
server.listen(port);
In order to listen on ipv4, I made the following change :
var server = http.createServer(app);
server.listen(port,"127.0.0.1");
Giving the try one more time the port was listening with ipv4, otherwise it is listening on ipv6 :
$ sudo netstat -lntp |grep -i 3000
tcp 0 0 127.0.0.1:3000 0.0.0.0:* LISTEN 20501/node /home/ml

This site can’t be reached on Nodejs Express?

I'm totally new to Node and I tried to run a test site on a hosting centos 7 (vultr.com). I've got nodejs, express installed.
Hello.js
const express = require('express')
const app = express()
app.get('/', function (req, res) {
res.send('Hello World!')
})
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
})
run node hello.js
On my PC, http://x.x.x.x:3000/ => shows This site can’t be reached
x.x.x.x took too long to respond.
UPDATE:
I think you should consider about your server port. Have you open port 3000 in CentOS?
You can check your open port by typing
iptables -L
I think the firewall blocked your port you can open it by type this command
iptables -A INPUT -p tcp --dport 3000 -j ACCEPT
If you are using remote server, probably your 8080 port is blocked.
If you have root access and port 80 is open you can try and run script with sudo
But the first option is probably your problem
If you use the Google Cloud platform, you can open port 3000 at FIREWALL RULES in VPC network.
It works for me.

Running multiple node express applications on single port

I am want to make proxy manger application for express,
but I have error all time that I try to do require for two diffrent applications.
i am using on app.use to route between the two application.
thanks alot
david
var app1 = require('./../app1/server/server');
var app2 = require('./../app2/server/server');
var app = require('express')();
app.use("/", app1);
app.use("/app2", app2);
app.listen(80, console.log("server up"))
Error is :
event,js:85 listen eaddrinuse
The port 80 is in use by another process. try with another empty port like 8080 or 3000. You can also try by stopping the application that is using port 80, then it will work.

Use a node app from another device locally

I have a server (10.0.0.12) and my laptop (10.0.0.2) on a local network.
When I run curl http://10.0.0.2:3000 on the server, it works fine. When I run curl http://10.0.0.12:3000 on my laptop, it doesn't work saying site is unavailable.
I am able to ping and ssh into the server from my laptop.
Here is my code to finish the connection:
app.set('port', (3000));
app.listen(app.get('port'), function(){
console.log("Node app running on localhost:" + app.get('port'));
}
I've tried passing in an ip address to the listen() function, but made no difference. I tried passing in 10.0.0.12 (the ip address of the server), 127.0.0.1, and 0.0.0.0 all with the same result.
How can I host my node app on a local network and have everyone who is on the local network be able to access it through the browser?
EDIT: I'm running on CentOS 7.
EDIT2: When I run netstat -lnt, it says this:
tcp 0 0 0.0.0.0:3000 0.0.0.0:* LISTEN
Have you tried to just omit the IP address? It should then be available on the IP address of the machine it is running on and the specified port.
As suggested by HA. remove the IP.
As you can see from the documentation:
https://nodejs.org/api/http.html#http_server_listen_port_hostname_backlog_callback
If the hostname is omitted, the server will accept connections directed to any IPv4 address (INADDR_ANY).
P.S. Which is the OS on the server?
Maybe you can try :
app.listen(3000, '0.0.0.0', function(){
console.log("Node app running on 0.0.0.0:3000");
}
A possible issue could be you aren't using the http module?
var http = require('http').Server(app);
http.listen(3000, function () {
console.log('App running on port 3000');
});
A good practice would be set the port like
app.set('port', (3000));
var http = require('http').Server(app);
http.listen(app.get('port'), function () {
console.log('App running on port ' + app.get('port'));
});

Can't connect to nodejs server

I run Apache on my server. Going to my address x.x.x.x:port loads the index.html page in /var/www. When I stop the server, I can no longer connect (all good).
Now I start the node server with node server.js (the server.js file below is also located in /var/www).
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(port, 'x.x.x.x');
console.log('Server running at http://x.x.x.x:port/');
This gives the error listen EADDRNOTAVAIL, but I am not running any other node server (there is no other process running at this port).
I have also tried omitting the IP address and just listening thus: listen(port);
This returns no errors, but I cannot connect to the server (Browser says: Firefox can't establish a connection to the server at x.x.x.x:p.)
I have found out the problem. You don't need to specify a host name:
listen(port, 'x.x.x.x')
should just be
listen(port)
otherwise the server will not accept any connection except ones directed at the specified ip.
The port is in use or not available. Try a different port like:
listen(88, 'x.x.x.x');
and see if that connects. Also, make sure that x.x.x.x is actually the ip address of your server. You can listen on all IPs by doing:
listen(88, '0.0.0.0');
or by leaving the host/ip section out entirely. If it does connect on another port, you just need to find what is using the port you want. If it's port 80, use:
sudo netstat -tulpn | grep :80
to get the program using that port.
Sounds like the port is locked up and in use..
The following command will give you a list of node processes running.
ps | grep node
To free up that port, stop the process using the following.
kill <processId>

Resources