Address is already in use after killing ports - node.js

I've killed this port 8080 a lot of times with npx kill-port 8080
but I still get an error saying:
Error: listen EADDRINUSE: address already in use :::8080
Here is the code
const app = express()
app.post("/dblwebhook", webhook.listener(async vote => {
console.log(`${vote.user} has voted me`)
}))
app.listen(8080)
Am I killing the port wrong? (This is on my centos 8 vps)

Related

NodeJS on Ubuntu 20.04 - Cannot start server because address in use (:::80) but I can't find any port listening to that address

When running the following command:
sudo node server/server.js
I receive the following error:
Listening on port 80 events.js:174
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE: address already in use :::80
at Server.setupListenHandle [as _listen2] (net.js:1280:14)
at listenInCluster (net.js:1328:12)
at Server.listen (net.js:1415:7)
at Function.listen (/home/app/node_modules/express/lib/application.js:618:24
However when looking at similar questions I was advised to find the process using the port and end it.
When running on root and regular user the following command:
lsof -n -i:80
as well as
netstat -tulpn | grep :80
I get 0 results and no output returned.
Killing the node daemon (using pkill node) and then restarting it didn't work either.
Changing the port gives the same error strangely:
Error: listen EADDRINUSE: address already in use :::8080
Why am I still getting this error?
EDIT The Server Code:
// optional: allow environment to specify port
const port = process.env.PORT || 80;
// wire up the module
const express = require("express");
var http = require("http");
var request = require("request");
// create server instance
const app = express();
// bind the request to an absolute path or relative to the CWD
app.use(express.static(path.join(__dirname, "../dist")));
app.use(express.json());
app.listen(port, () => {
console.log(`This app is listening at http://localhost:${port}`);
});
...
Hi can you upload the server.js content .
You have to run your http server in a other port or try to stop nginx or Apache2 service if you installed them
Simply Kill the Process::
sudo kill $(sudo lsof -t -i:8080)
where replace 8080 with your address and then rerun your app.
If you want to know all the port or address in use simply install nmap from ubuntu snap store and scan all the port using terminal with command
nmap localhost

Trouble conecting to Node Server on EC2 Instance

I have a Node application running on an EC2 instance, and after npm start, I am unable to connect to the application via the web, but everything indicates that it is running correctly.
Here's how my server.js looks like:
app.listen(8000,"0.0.0.0", () => {
console.log("app running on port 8000");
});
How are the inbound rules of the EC2 security group:
I saw on other sites that removing " 0.0.0.0. " From app.listen and leaving only the port would solve the problem, but unfortunately that didn't work.
Another thing I tried was to add a * Custom TCP * rule to port 8000 in the security group, but this prevented the application from running, saying that the port was already in use
Something is already listening on port 8000, a netstat should tell you what it is.
Simply add a TCP security group for port 8001 or 8080 and in your code use that port instead.

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

What else is required to access a node app from outside a Digital Ocean droplet?

We have set up a node server which runs on port 5000.
In a newly created droplet, we have installed and started nginx. To access the node app, we have changed the default port from 80 to 5000 in /etc/nginx/sites-enabled/default
server {
listen 5000 default_server;
listen [::]:5000 default_server;
ufw is enabled
sudo ufw enable
Also the port is enabled
sudo ufw allow 5000/tcp
Also, tried this way too:
sudo ufw allow 5000
As confirmed with sudo ufw status
netstat -ntlp
Also the app is configured to listen on the public interface
const server = app.listen(process.env.PORT || 5000, '0.0.0.0', () => {
console.log('Express server listening on port %d in %s mode', server.address().port, app.settings.env);
});
However, not even the default port was responding. Hence, we reverted to 80 as the default port.
What else is required to access node app outside of the droplet?
When it comes to NodeJS and NGINX, we'll want to configure NGINX to listen on port 80, though we'll want to use proxy_pass to pass the request from the web server (NGINX) to the NodeJS application on the port that the application is running on. This will allow us to keep the port out of the URL.
With the current configuration, NGINX would be listening on port 5000 which would prevent the application from being able to listen in on the same port (or vice versa).
There is an excellent guide that covers setting up NodeJS + NGINX -- this specific part is the most important:
https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-16-04#set-up-nginx-as-a-reverse-proxy-server
The above covers how we'd go about setting up the Server Block :-)

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.

Resources