node.js application deployment in aws in CentOs not working - javascript

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

Related

Ubuntu open/unblock ports for nodejs

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.

Azure Web Site starting my Hapi Node.js site with socket protocol

Whenever I deploy my Hapi.js web application to azure, it starts the server using the socket protocol (see output below).
socket:\\.\pipe\b5c0af85-9393-4dcb-bd9a-3ba9b41ed6fb
GET /
GET /{param*}
GET /api/employees
POST /api/employees
GET /api/employees/{id}
PUT /api/employees/{id}
DELETE /api/employees/{id}
POST /api/worklog
GET /login
POST /login
Hapi server started # socket:\\.\pipe\b5c0af85-9393-4dcb-bd9a-3ba9b41ed6fb
150914/214730.270, [response], socket:\\.\pipe\b5c0af85-9393-4dcb-bd9a-3ba9b41ed6fb: [1;32mget[0m / {} [32m200[0m (316ms)
However, whenever I am running this locally, it starts using http... I have not run into this issue using express or loopback, only Hapi. Is there some sort of configuration that I am missing? This is the server.connection function:
var server = new Hapi.Server();
var host = process.env.host || '0.0.0.0';
var port = process.env.port || 3000;
server.connection({host: host, port: port});
The reason this is a big deal is because I cannot pass socket://*<mydoamin>* to google as a callback URI for OAuth.
You shouldn't need to pass socket://<domain> to google, you'd pass the normal https://yourDomain.com or even the https://yourSiteName.azurewebsites.net to Google for OAuth callback and it should work as you would expect.
The fact that the node application is listening on a pipe rather than a normal tcp socket is just an implementation detail of iisnode. Basically the problem is that node has it's own webserver so you can't use it with other webservers like IIS, Apache, nginx, etc. iisnode bridges the gap between IIS and node in that it allows IIS to listen to the HTTP port on the machine 80 and when IIS gets a request on that port, it just forwards it to the node process that's listening on a named pipe. This allows you to manage your sites in IIS as you normally would on a Windows Server machine, while actually writing your app in node.
You can think of it as 2 webservers running on the box, one (IIS) is acting as a proxy for the other (node) where all the work is actually happening. The fact that the iisnode developer chose to use a named pipe instead of a normal tcp socket is odd (though kind of understandable since you can't easily reserve a port per se as you can a pipe), but it's the way it is.

OpenShift NodeJS deployment : socket.io index.html port assignment, etc

I locally wrote a nodeJS app using socket.io and express modules.
I wanted to use openshift for hosting.
So I changed the main .js to server.js which seems to be the index equivalent of the openshift file and changed the server port setting to:
var server = require('http').createServer(app).listen(process.env.OPENSHIFT_NODEJS_PORT || 3000);
as indicated in some posts.
However after git commit, I am still getting:
remote: info: socket.io started
remote: warn: error raised: Error: listen EACCES
remote: DEBUG: Program node server.js exited with code 0
remote:
remote: DEBUG: Starting child process with 'node server.js'
and the website doesn't work.
As the app is serving a html file, there are two more places, where the port is mentioned, which sit in the index.html that is served:
header:
<script src='//localhost:3000/socket.io/socket.io.js'></script>
and within javascript for the html file:
var socket = io.connect('//localhost:'+process.env.OPENSHIFT_NODEJS_PORT || 3000);
// intial vars and multi list from server
socket.on('clientConfig', onClientConfig);
All files and modules are seemingly uploaded, but the EACCES error still prevails.
I get the feeling that maybe the header link to localhost:3000 might be the skipping point, but I am not sure. Anyone have any idea, what the problem is?
Also, there is no : socket.io/socket.io.js file in the socket.io modules folder, which I find confusing.
I had recently developed a chat client application using socket.io and also had webrtc in it. I was able to deploy the app on openshift by making the following changes into code.
Client Side
Keep the include script tag in a relative manner like so
<script src="/socket.io/socket.io.js"></script>
While declaring io.connection, change the ip part to point the application to server in this format.
var socket = io.connect('http://yourapp-domain.rhcloud.com:8000/', {'forceNew':true });
8000 is for http and 8443 is for https
Server Side
The io and the server should both be listening on the same port and the order in which the statements are run should also be given attention.
Step 1: Declare the http server using app.
( app is obtained from express)
var express = require('express');var app = express();)
var server = require('http').Server(app);
Step 2:
Declare io from socket.io and combine it with the server object.
var io = require('socket.io').listen(server);
Step 3:
Now, allow the server to listen to openshift port and ip.
server.listen(process.env.OPENSHIFT_NODEJS_PORT, process.env.OPENSHIFT_NODEJS_IP);
Please pay special attention to the order of the statements you write, it is the order which causes issues.
The server side of your websocket needs to listen on port 8080 on your openshift ip address, the CLIENT side needs to connect to your ws://app-domain.rhcloud.com:8000
I have a few notes on how to use WebSockets here: https://www.openshift.com/blogs/10-reasons-openshift-is-the-best-place-to-host-your-nodejs-app#websockets
You don't need any additional server-side changes after adapting your code to take advantage of environment variables (when available)
OpenShift's routing layer exposes your application on several externally-accessible ports: 80, 443, 8000, 8443.
Ports 8000 and 8443 are both capable of handling websocket connection upgrades. We're hoping to add support for WebSocket connections over ports 80 and 443 soon.

Dotcloud www and TCP in single app -

I'm trying to get a nodejs socket server running that will allow remote communication between two clients running a Flash game that communicates using a custom protocol. Due to Flash security restrictions, it seems that the socket server must be running on the same host as the web server that servers the Flash game. I've been continuously getting the following error:
The service crashed at startup or is listening to the wrong port. It failed to respond on port "nodejs" (8080) within 30 seconds
What I need is a way to run my nodeJS server code, while simultaneously serve the flash files.
I'm using the environment JSON variables to determine what port to listen on, and my YML is similar to the one discussed here but no luck...
Just wondering if I can get some info on how to create a working socket server/web server that will work for this (or if it is actually possible)
You can use the following dotcloud.yml file:
www:
type: nodejs
ports:
mything: tcp
Then in your Node.js app, you can bind a HTTP server to port 8080, and an arbitrary TCP server to the port contained by environment variable $PORT_MYTHING. Then run dotcloud info on your service; in the ports section, you will see something like this:
- name: mything
url: tcp://myapp-johndoe.dotcloud.com:12345
From now on, if you connect to myapp-johndoe.dotcloud.com on port 12345, you will actually connect to $PORT_MYTHING in your application.
I hope that it makes sense, and that it is what you were looking for!

How could others, on a local network, access my NodeJS app while it's running on my machine?

I have a pretty straight-forward question. I made a web game with NodeJS, and I can successfully play it by myself with multiple browser windows open side-by-side; however, I'd like to know if it's possible for other local machines to be able to access and play the game with me too.
I naively tried using this url: my-ip-address:8000 and it won't work.
Your node.js server is running on a port determined at the end of the script usually. Sometimes 3000. but can be anything. The correct way for others to access is as you say...
http://your.network.ip.address:port/
e.g.
http://192.168.0.3:3000
Check you have the correct port - and the IP address on the network - not the internet IP.
Otherwise, maybe the ports are being blocked by your router. Try using 8080 or 80 to get around this - otherwise re-configure your router.
If you are using a router then:
Replace server.listen(yourport, 'localhost'); with server.listen(yourport, 'your ipv4 address');
in my machine it is
server.listen(3000, '192.168.0.3');
Make sure yourport is forwarded to your ipv4 address.
On Windows Firewall, tick all on Node.js:Server-side JavaScript.
I had the same question and solved the problem. In my case, the Windows Firewall (not the router) was blocking the V8 machine I/O on the hosting machine.
Go to windows button
Search "Firewall"
Choose "Allow programs to communicate through Firewall"
Click Change Setup
Tick all of "Evented I/O for V8 Javascript" OR "Node.js: Server-side Javascript"
My guess is that "Evented I/O for V8 Javascript" is the I/O process that node.js communicates to outside world and we need to free it before it can send packets outside of the local computer. After enabling this program to communicate over Windows firewall, I could use any port numbers to listen.
One tip that nobody has mentioned yet is to remember to host the app on the LAN-accessible address 0.0.0.0 instead of the default localhost. Firewalls on Mac and Linux are less strict about this address compared to the default localhost address (127.0.0.1).
For example,
gatsby develop --host 0.0.0.0
yarn start --host 0.0.0.0
npm start --host 0.0.0.0
You can then access the address to connect to by entering ifconfig or ipconfig in the terminal. Then try one of the IP addresses on the left that does not end in .255 or .0
Faced similar issue with my Angular Node Server(v6.10.3) which set up in WIndows 10.
http://localhost:4201 worked fine in localhost. But http://{ipaddress}:4201 not working in other machines in local network.
For this I updated the ng serve like this
//Older ng serve in windows command Prompt
ng serve --host localhost --port 4201
//Updated ng serve
//ng serve --host {ipaddress} --port {portno}
ng serve --host 192.168.1.104 --port 4201
After doing this modification able to access my application in other machines in network bt calling this url
http://192.168.1.104:4201
//http://{ipaddress}:4201
The port is probably blocked by your local firewall or router. Hard to tell without details.
But there is a simple solution for which you don't have to mess with firewall rules, run node as a privileded process to serve on port 80, etc...
Check out Localtunnel. Its a great Ruby script/service, which allows you to make any local port available on the internet within seconds. It's certainly not useful for a production setup, but to try out a game with colleagues, it should work just fine!
const express = require('express');
var app = express();
app.listen(Port Number, "Your IP Address");
// e.g.
app.listen(3000, "192.183.190.3");
You can get your IP Address by typing ipconfig in cmd if your Windows user else you can use ifconfig.
After trying many solution and lot of research I did to the following to make sure my localhost is accessible from other machine in same network. I didn't start my server with IPAddress as parameter to listen method as suggested by others in this question. I did the following to make sure my local node js server is accessible from other machine on same local network. My node server is running in Windows 10 machine.
Open "Windows Defender Firewall with Advanced Security"
Select "Inbound Rules" in the left pane.
In the list of available rules, "Node.js Server-side Javascript" has "Block the connection" radio checked. Change this to "Allow the connection".
Please see the attached screenshot:
After these changes, I am able to access my localhost using http://IPAddress:Port/
Thanks.
And Don't Forget To Change in Index.html Following Code :
<script src="http://192.168.1.4:8000/socket.io/socket.io.js"></script>
<script src="http://code.jquery.com/jquery-1.6.2.min.js"></script>
var socket = io.connect('http://192.168.1.4:8000');
Good luck!
This worked for me and I think this is the most basic solution which involves the least setup possible:
With your PC and other device connected to the same network , open cmd from your PC which you plan to set up as a server, and hit ipconfig to get your ip address.
Note this ip address. It should be something like "192.168.1.2" which is the value to the right of IPv4 Address field as shown in below format:
Wireless LAN adapter Wi-Fi:
Connection-specific DNS Suffix . :
Link-local IPv6 Address . . . . . : ffff::ffff:ffff:ffff:ffad%14
IPv4 Address. . . . . . . . . . . : 192.168.1.2
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Start your node server like this : npm start <IP obtained in step 1:3000> e.g. npm start 192.168.1.2:3000
Open browser of your other device and hit the url: <your_ip:3000> i.e. 192.168.1.2:3000 and you will see your website.
put this codes in your server.js :
app.set('port', (80))
app.listen(app.get('port'), () => {
console.log('Node app is running on port', app.get('port'))
})
after that if you can't access app on network disable firewall like this :
ngrok allows you to expose a port on the internet with custom forwarding refs:
$ npx ngrok http 8000
First, check your ipv4 address. In my case my ipv4 address is 192.168.44.112. If you don't know your ipv4 address, run this command on cmd.
ipconfig
Follow this code...
const express = require('express');
const app = express();
const port = process.env.port || 8000
app.get('/', (req, res) => {
res.send("Hello Network!")
});
app.listen(port, '192.168.77.112', ()=>{
console.log(`Listening port on ${port}`)
});
In Ubuntu you can fix this by allowing a specific port or port range:
sudo ufw allow PORT-NUMBER/tcp
example:
sudo ufw allow 3000/tcp
or a range:
sudo ufw allow 3000:3001/tcp
var http = require('http');
http.createServer(function (req, res) {
}).listen(80, '127.0.0.1');
console.log('Server running at http://127.0.0.1:80/');
By default node will run on every IP address exposed by the host on which it runs. You don't need to do anything special. You already knew the server runs on a particular port. You can prove this, by using that IP address on a browser on that machine:
http://my-ip-address:port
If that didn't work, you might have your IP address wrong.
I had this problem. The solution was to allow node.js through the server's firewall.

Categories