Ubuntu open/unblock ports for nodejs - javascript

I'm trying to listen port 8080 for socket.io but I get error:
http://localhost:8080/socket.io/?EIO=3&transport=polling&t=Lai1FQh net::ERR_CONNECTION_REFUSED
I'm using ubuntu virtual machine from digitalocean and first when I ssh login I get message:
The "ufw" firewall is enabled. All ports except for 22, 80, and 443 are BLOCKED.
I'm beginner with this but doesn't that mean that port 8080 is blocked.
How do I fix this and what I need to do
Here is my code for server:
var socket = require( 'socket.io' );
var express = require( 'express' );
var http = require( 'http' );
var app = express();
var server = http.createServer( app );
var io = socket.listen( server );
server.listen(8080, function(){
console.log("Listening");
})
In my website i just connect to that port like this:
<script src="node_modules/socket.io-client/dist/socket.io.js"></script>
<script>
var socket = io.connect( 'http://localhost:8080' );
...
<script>

The error message you get means that ufw (it's a firewall) is blocking all ports except those 3.
To enable port 8080:
sudo ufw allow 8080
Then reboot your server.
(As seen from the guide available on your hosting provider website)

You probably have another instance of your program running, or some other program that uses that port. Run:
ps aux
in your shell to see what processes are running and kill those that shouldn't be running. You can also run lsof or netstat to see what is listening on which port. See the manuals for those commands.
If you can open 8081 or something like that but not 8080 then it means that something is already listening on port 8080. You can verify it with trying to connect to that port with curl, wget, netcat or by using nmap. See their man pages for details.

Related

how to bind to privileged port 443 using node on Windows Server

First post on SO! When trying to start my node server I'm getting an unhandled exception:
throw er; // Unhandled 'error' event
Error: listen EACCES: permission denied 0.0.0.0:443
How can I enable node server to run on a 'privileged' port?
When setting the port to anything above 1000, everything works perfectly, but this is problematic because of customer regulations on which ports can be open, so I'm trying to stick to 443.
I see others have solved this using sudo for Linux environments, and I tried running mine as Administrator but had no luck.
Client connects with
ws = new WebSocket('wss://subdomain.website.com');
ws.onopen = function() {
ws.close();
}
node server starts with
'use strict'
var detectedSampleRate = 0;
const https = require("https");
const fs = require("fs");
const options = {
key: fs.readFileSync("privateKey.key"),
cert: fs.readFileSync("cert.pem")
};
var express = require('express');
const app = express();
var session = require('express-session');
app.use(express.static(__dirname + '/public'));
app.use(session({
secret : '##########',
resave : false,
saveUninitialized: true
}));
var server = https.createServer(options,app).listen(443);
Additional info:
Website hosted on IIS serves index.html to user over https.
index.html then opens a websocket connection to a node server running on the IIS server.
index.html and node server talk to each other.
(IIS) --> index.html
node <--> webconnection within index.html
The client side of this connection that's served to the user is working correctly, and calls the connection via 443 with proper certificates.
The server side (node server) fails to start due to this permission issue.
Thank you in advance!
I ended up figuring it out. Turns out the IIS server was hogging the 443 port and when node gives the error
throw er; // Unhandled 'error' event
Error: listen EACCES: permission denied 0.0.0.0:443
It meant that the port was currently in use.
Because of my strange setup, I ended up leaving the index.html to be served from IIS, and the internal webconnection to connect with the node server running on a separate machine within the same domain.
For others, if you run into this unhandled exception on Windows, check if you're doing anything weird with your implementation, or if the port is being used by anything else.

SocketIO is not working in Heroku Environment [Works in Localhost]

I developed chat program with NodeJS and SocketIO. Then I deployed to Heroku Server and I found that SocketIO is not working in Heroku.
I already checked the log in Heroku but there are nothing. But it is weird that it is working in localhost but not in Heroku environment. I think there are some problem with PORT Setting.
[Server Side Code]
/* Socket IO Settings */
const server = require('http').Server(express);
const io = require('socket.io')(server);
let port = process.env.PORT || 3001;
server.listen(port, function () {
console.log(`SocketIO Listening on port : ${port}`)
});
[Client Side Code]
/* Chat Functions */
var socket = io.connect(`${window.location.hostname}:3001`);
At first time I just use var socket = io.connect('http://localhost:3001'); code and it works well in Localhost Env. But after I deployed to heroku I add Heroku Variable to use it in production.
[PORT Setting]
NODEJS Server : 3000
SOCKET IO : 3001
Heroku process.env.PORT : 3001 (But When I check Heroku console it changes everytime when I reload the dynos and in Logs said me that the ports are already in use.)
I already read Heroku Document but it seems that I create NodeJS App with Express Generator I think it should be different solution. I already tried heroku document but it's not working
Set your server ip in server.listen function
var ip='127.0.0.1';
var port ='3001' ;
server.listen(ip,port, function() {console.log('server stsrt in ip: ' +ip+' and port: ' +port);}) ;

Run Node.js on a Google Compute Engine Debian server

I have a debian server running on Google Compute Engine with a host like example.com and I'm trying to run a node.js app on a directory like example.com/mynodeapp.
Node.js, NPM are installed properly on the debian server.
I'm running pm2 start main.js from the root of example.com/mynodeapp and everything running but when I go to example.com/mynodeapp, I have nothing, just the indexing of the files.
Express.js configure
main.js (entry)
var express = require('express')
var vhost = require('vhost')
express()
.use(vhost('example.com/mynodeapp', require('./dist/index').app))
.listen(8080)
dist/index.js
var express = require('express')
var app = express()
app.get('/', function(req, res) {
res.send('Hello World!');
})
exports.app = app
With .listen(8080) the port is set to 8080, so you'll have to change that or try example.com:8080.
Note that you will run into one of two problems, depending on your choice: Port 8080 is probably not open – you'd have to allow it in the firewall.
If you're currently getting a file listing on port 80, there's some other server running (possibly apache or nginx from the standard debian install). You will have to stop that server to free up the port.

node.js application deployment in aws in CentOs not working

when i run a node.js application in CentOs 6.5 in AWS
var sys = require( "sys" );
var http = require( "http" );
// Create our HTTP server.
var server = http.createServer(
function( request, response ){
// Create a SUPER SIMPLE response.
response.writeHead( 200, {"content-type": "text/plain"} );
response.write( "Hellow world from AWS!\n" );
response.end();
}
);
// Point the HTTP server to port 8080.
server.listen( 3000 );
// For logging....
console.log( "Server is running on 3000" );
it runs and shows this on console:
Server is running on 3000
but when i open my browser and run the
public DNS given by amazon
:http://ec2-54-152-55-189.compute-1.amazonaws.com:3000/, it shows webpage not available ,but in terminal of CentOs in aws ,when I run command :curl http://ec2-54-152-55-189.compute-1.amazonaws.com:3000/ it shows
Hellow world from AWS!
1)Inbound rules are
HTTP -- AnyWhere
SSH -- AnyWhere
CUSTOM TCP RULE (PORT-3000)-- AnyWhere
HTTPS-- AnyWhere
CUSTOM UDP RULE (PORT-3000)-- AnyWhere
2)Outbound Rules are
All traffic| All protocol |ALL port| AnyWhere
Any help is appreciated. Thanks a lot.
the issue is resolved ,even setting inbound rules as follows:
HTTP -- AnyWhere
SSH -- AnyWhere
CUSTOM TCP RULE (PORT-3000)-- AnyWhere
HTTPS-- AnyWhere
CUSTOM UDP RULE (PORT-3000)-- AnyWhere
in centOS 6.5 instance ,firewall was not allowing any connection from port (HTTP)80,(CUSTOM)3000,(HTTPS)443.Therefore,i made allow the required port no. in iptables
see:
http://www.cyberciti.biz/faq/howto-rhel-linux-open-port-using-iptables/ to edit the iptables and restarted the firewall
https://www.digitalocean.com/community/tutorials/how-to-set-up-a-basic-iptables-firewall-on-centos-6 to allow particular port in firewall

Socket.io initialization resulting in a 404 network error

I am using nodejs and express to create a basic chat app and I'm getting a network 404 error message when trying to initialize the socket.io object.
<script src="/javascripts/socket.io/socket.io.js-client"></script>
<script type="text/javascript">
var socket = io.connect();
</script>
Tha above code results in a 404 error for some polling call
"NetworkError" 404 Not Found - http://localhost:3000/socket.io/?EIO=3&transport=polling&t=1432851505880-89
I need the polling to run using the following url instead: http://localhost:3000/javascripts/socket.io/?EIO=3&transport=polling&t=1432851505880-89
because I am using express, but not sure how to accomplish this.
Server Side Code:
var app = express();
var server = require( "http" ).createServer( app );
var io = require("socket.io").listen(server);
server.listen(8888);
io.sockets.on('connection', function(socket){
socket.on('send message', function(data){
io.sockets.emit('new message', data);
});
});
module.exports = app;
I have been trying to troubleshoot this one for quite a while now with no success. I appreciate any advice. Commenting out the var socket = io.connect() resolves the error. Appreciate any advice.
Thanks
Your web page is apparently running on port 3000, but your socket.io server is listening on port 8888. The two must be the same port so it is no surprise that there is no response for a socket.io request on port 3000 (since your socket.io server is listening on port 8888).
Because the default URL it is trying is port 3000, then that must be the port that your web page is one and it must be served by a different web server. If that is the case, then you will need to do one of two things:
Combine the web server that serves your web pages with the socket.io server so the same server is taking care of both.
Specify the port in the client request and enable your socket.io server for cross-origin requests.
To specify the port in the client request, you can do this:
<script src="http://myserver.com:8888/socket.io/socket.io.js"></script>
<script type="text/javascript">
var socket = io("http://yourserver.com:8888");
</script>
Note, that this is requesting the client socket.io library from the express server where your socket.io server is (so it's using the same port that your socket.io server is running on).
If the HTML where socket.io client javascript is being served is not also being served by express at port 8888 (it looks like the html is coming from port 3000), you may just need to configure the socket.io client to point at the port where the server-side socket.io has been setup to listen (8888):
var socket = io.connect('localhost:8888');

Categories