Socket EADDRINUSE Error - node.js

I am trying to set up a working nodeJS socket.io server with express. However, I keep returning this error:
Error: listen EADDRINUSE :::80
I have tried other solutions on this site which told me to search for and kill a process that is already running, but the grep commands return no running process. I can only assume the error is with my setup code:
var express = require("express"),
http = require('http');
var app = express();
var server = http.createServer(app)
var io = require('socket.io')(server, {path: '/folder/socket.io'}).listen(server);
app.get("/folder/", function(req, res){
res.sendFile(__dirname + "/folder/client/index.html");
});
app.use("/folder/client", express.static(__dirname + "/folder/client"));
app.listen(80);
Note that I specify the path folder because I want node to run on a subdirectory instead of the main path /. Is there something I'm doing wrong?

You should use
server.listen(80);
instead of
app.listen(80);
Hope that helps !

What I do on my cloud instances is I redirect port 80 to port 3000 with this command:
sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 3000

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

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.

Nodejs openshift app deployed code not working

I am working on nodejs app,i wrote server listening code,then i deployed code into openshift hosting,there i am getting 503 response,in local if i run this code it is properly working.
code:
var http = require('http');
var express = require('express');
var fs = require('fs');
var app = express();
app.set('port', process.env.OPENSHIFT_NODEJS_PORT || 8080);
app.set('ip', process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1');
http.createServer(app).listen(app.get('port'), app.get('ip'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
app.get('/', function (req, res) {
res.send('Hello World!');
});
Do you pass the OPENSHIFT_NODEJS_IP variable?
Quite often the problem is that the nodejs application is listening on IP 127.0.0.1 like in this case, but should on 0.0.0.0.
If that doesn't help please provide more details on how you created the application in OpenShift.
Here are a few tricks that might help determine what the issue is. The first way I approach it is to ssh into the OpenShift cartridge. Assuming the name of your application is hello_world:
rhc ssh -a hello_world
Once inside, type:
lsof -i | grep node
This should list out the running node processes along with the ips and ports that each is listening to. I have two node applications running, so my output looks like this:
node 23028 3389 11u IPv4 518408054 0t0 TCP 127.6.158.129:8097 (LISTEN)
node 23028 3389 12u IPv4 518408056 0t0 TCP 127.6.158.129:8080 (LISTEN)
So my node applications are listening on an actual ip address other than localhost. My guess is that you will see something to this effect:
node 23028 3389 11u IPv4 518408054 0t0 TCP 127.0.0.1:8080 (LISTEN)
Next, check your environment:
env | grep OPENSHIFT_NODEJS
Mine looks like this:
OPENSHIFT_NODEJS_PORT=8080
OPENSHIFT_NODEJS_IP=127.6.158.129
So I can see that the OPENSHIFT_NODEJS_PORT and OPENSHIFT_NODEJS_IP matches the output of my lsof statement above, so I'm all good.
I'm sure you will see a discrepancy after doing all this, and that will be your clue.

Running a simple script on a web server using NodeJS

I'm trying to make a simple JS script run on my web server using NodeJS :
var http = require('http');
var server = http.createServer(function(req,res) {
res.writeHead(200,{"Content-Type":"text/plain"});
res.end("Hello World!");
});
server.listen(8000);
console.log('Server running');
Output when I run this on local :
curl http://127.0.0.1:8000
Hello World!
However, when I try to curl onto my web server it timeout.
I don't know if it's important or not but I have apache2 installed on the web server. Any help would be very much appreciated.
You can't access node.js from outside because it is listening on localhost IP i.e 127.0.0.1. You need to configure node.js to listen on 0.0.0.0 so it will be able to accept connections on all the IPs of your machine.
Try this:
server.listen(8000, "0.0.0.0");
If it still doesn't work and you are using iptables you may have to open port 8000. Try this:
iptables -I INPUT -p tcp --dport 8000 -j ACCEPT

How can I configure expressjs to handle both http and https?

I've scoured stackoverflow and the express google group, but I'm still coming up short.
From what I gather, I can do one of two things:
1) create an instance of an http server and an https server and set the two to listen to two different ports. In the routes, redirect the http request to the https port.
//app
var app = express.createServer();
var app_secure = express.createServer({key: key, cert: cert});
app.listen(8080);
app_secure.listen(8443);
//routes
app.get("unsecure/path", function(req, res) {
...
}
app.get("secure/path", function(req, res) {
res.redirect("https://domain" + req.path);
}
app_secure.get("secure/path", function(req, res) {
res.send("secure page");
}
2) do what TJ Hollowaychuk says: https://gist.github.com/1051583
var http = require("http");
var https = require("https");
var app = express.createServer({key: key, cert: cert});
http.createServer(app.handle.bind(app)).listen(8080);
https.createServer(app.handle.bind(app)).listen(8443);
When I do 1, there are generally no problems. However, it feels clunky to manage two servers and I really feel like there should be a better way.
When I do 2, I get this:
(node SSL) error:1408A0C1:SSL routines:SSL3_GET_CLIENT_HELLO:no shared cipher
Of course, I can just default to option 1, but I really, really want to know why I'm getting that "no shared cipher error" when I do option 2. And option 2 would be my preferred route.
Following #ypocat 's comment you can enable https in your express.js application like so
var http = require('http');
var https = require('https');
var express = require('express');
var fs = require('fs');
var app = express.createServer();
// cutomize your app as ususal
app.configure( function () { ... });
app.configure('production', function () { ... });
// ....
// attach express handler function to TWO servers, one for http and one for https
http.createServer(app.handle.bind(app)).listen(8080);
https.createServer({
ca: fs.readFileSync('./server.ca-bundle'),
key: fs.readFileSync('./server.key'),
cert: fs.readFileSync('./server.crt')
}, app.handle.bind(app)).listen(8081);
Note that you should receive server.ca-bundle, server.key and server.crt from a certificate authority.
Also as you will probably run node without sudo you need to make sure port 80(http) and 443(https) are open
# in Ubuntu
sudo ufw status
sudo ufw allow 80
sudo ufw allow 443
and to forward requests on 8080 to 80 and from 8081 to 443 respectively
# in Ubuntu
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 443 -j REDIRECT --to-port 8081
Hope this helps
Is your certificate an RSA certificate rather than a DSA one? It sounds like the ciphers your browser supports are not supported by your nodejs server - you many need to update your OpenSSL and recompile NodeJS?

Resources