NodeJs (Express) wont listen to the IP and host in ubuntu - node.js

I have deployed my app to ununtu.
this is the bin/www:
app.set('port', 3000);
app.set('host','app.site.com');
var server = http.createServer(app);
server.listen(port,'64.143.255.122');//I put here a fake IP (deployed with the real one)
server.on('error', onError);
server.on('listening', onListening);
I have created a host in my PC: 64.143.255.122 app.site.com and I open the browser in: http://app.site.com:3000 and it does not work.
But, if I go via lynx, inside the server, and write lynx http://localhost:3000 it will work, I will get the correct page.
What might be the problem?
Thanks

Did you make iptables to allow it?
try this:
iptables -A INPUT -p tcp --dport 3000 -j ACCEPT

Related

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

Node.js can access default port, but can't access specific port

http.createServer(function(request, response){
response.writeHead(404, {'Content-Type': 'text/plain'});
response.write('Helloworld!');
response.end();
}).listen(3000,function(){
console.log("server listen on port::: 3000");
});
Here above is a quite simple snippet run in Ubuntu.
I can access normally by default port(80), but can't access by a specific port.
What I accessed is an external IP, not internal localhost.
I can get correct response via command line of "curl x.x.x.x:3000", but I just can't open it in web browsers.
Anybody who can give a hint will be appreciated!!!
A process is probably using the port.
netstat -tulpn | grep 3000 will give you what process is using the port.
If none found, look into your iptables.
Edit: As suggest from comment, probably OP desk that restrict external request for non http ports. Simple iptable will do.
-A OUTPUT -d **dest_ip**/32 -p tcp -m tcp --dport 3000 -j ACCEPT
Replace dest_ip by your ip.

Cannot connect to EC2 Instance through HTTP

I'm having trouble accessing my website through HTTP with an EC2 instance. I've tried changing the security setting by allowing HTTP on port 80, but it still doesn't work. SSH however is working fine. What could be the issue?
This is embarrassing, but the reason I couldn't connect to my EC2 instance was because my node js app never actually started a server for me to listen on port 80.
Adding this simple snippet of code
var server = app.listen(8080, function () {
var host = server.address().address
var port = server.address().port
console.log('Example app listening, host, port)
})
Along with this shell command for redirecting fixed the issue.
iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to 8080
Could check ip-tables if it is blocking 80, check the security group if the TCP for PORT 80 is open to your IP address or ( 0.0.0.0/0 ).
Given that you are able to SSH but not http, check if you can replicate the same settings in the SG which has the config for SSH 22 - also kindly look if the web-server ( or process ) is up and running in PORT 80.
First, login that instance, and run the command to confirm http service is running.
telnet INSTANCE_public_DNS_name 80
Then run the same command for your remote machine to confirm if there are any firewall issues.
If there is problem to telnet, then click the instance name, and go to description --> Security groups --> view rules.
You should see the port opened. If not, create a new security group or edit exist security groups, and assign to that instance.

Resources