Nodejs on linux not accessible outside Server - node.js

tried everything...
port 8008 seems to be open but no luck.
netstat shows 8008 to be listening
I can do curl localhost:8008 but not from an external machine using the ip address of my server
and yes, i want to host my nodejs on port 8008 (not 8080 - im using 8080 for something else)
netstat output:
tcp 0 0 0.0.0.0:8008 0.0.0.0:* LISTEN

Have you tried changing your port
var port = process.env.PORT || 8008; //server.listen(3000, '0.0.0.0');
app.listen(port, 0.0.0.0, function() {
console.log("Listening on " + port);
});
If you have .env insert value PORT=8008
and run your server

Related

Cant access nodejs server using machine ip address instead of localhost

I am using windows 10 as OS
Iam trying to access my node.js server from another device browser in the same network using my device ip address in our network--> ipv4 192.168.X.X " its obtaind using ipconfigin cmd" and port "4000", i did the following configration in my code:
module.exports = {
server: app,
start: (port) => {
const PORT = port || process.env.PORT || 4000;
server.listen(PORT,'0.0.0.0', () => { console.log(`Listening on port ${PORT}`); });
},
};
I also opened the inbound ports from windows firewall rules for ports 80, 443, 4000.
FireWall Rules Image
When I tried: http://localhost:4000/ -->it works.
When I tried: http://192.168.X.X:4000/ -->it didn't work.
The weird thing is that when I run the same setup on Linux "ubuntu" it works with the same code above "after opening the ports using: ufw allow 80,443,4000 proto tcp".
For debugging with cmd I tried:
netstat -a -o and I got that node is working in localhost:4000 rather than 0.0.0.0:4000
EX: TCP 127.0.0.1:4000 DESKTOP-T18TEC0:0 LISTENING 11628
How I can make it 0.0.0.0:4000 so I can access node.js server by device-network IP 192:168.X.X?
Ping my ip "192.168.X.X" and its pingable.

How do I set node app to run on a static port number on heroku?

my node app runs on port 8083 locally. when I push to heroku, how can I configure the service to run on the same port?
You cannot do it. Your code should listen on the port that you have in the PORT environment variable passed to you by the Heroku server that you can access as process.env.PORT and Heroku will listen on the outside on port 80 for HTTP and 443 for HTTPS.
See the docs:
https://devcenter.heroku.com/articles/runtime-principles
In particular:
https://devcenter.heroku.com/articles/dynos#web-dynos
https://devcenter.heroku.com/articles/http-routing
https://devcenter.heroku.com/articles/ssl-endpoint
Correct example:
// Get the port:
const PORT = process.env.PORT || 3000;
// Listen on the port:
app.listen(PORT, () => console.log('Listening on', PORT));
The default (3000 in this example) is for situations when you run it outside of Heroku (like for testing). When it is run on Heroku it should always listen on the port provided by Heroku. If it listens on some other port then Heroku will not proxy the traffic to your app correctly.

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.

Nodejs port 8000 ,4000 not working on server

I am using nodejs for rest-api and my app running on 2000,3000 but when i use port 8000 it work on browser but it not working on iphone also i was opened port 8000 from whm.
Try to run your application on 0.0.0.0 ip address, this guarantees it would be listening on all the configured network interfaces
var server = app.listen(8000, '0.0.0.0', function () {
var host = server.address().address;
var port = server.address().port;
console.log('App listening at http://%s:%s', host, port);
});

Use a node app from another device locally

I have a server (10.0.0.12) and my laptop (10.0.0.2) on a local network.
When I run curl http://10.0.0.2:3000 on the server, it works fine. When I run curl http://10.0.0.12:3000 on my laptop, it doesn't work saying site is unavailable.
I am able to ping and ssh into the server from my laptop.
Here is my code to finish the connection:
app.set('port', (3000));
app.listen(app.get('port'), function(){
console.log("Node app running on localhost:" + app.get('port'));
}
I've tried passing in an ip address to the listen() function, but made no difference. I tried passing in 10.0.0.12 (the ip address of the server), 127.0.0.1, and 0.0.0.0 all with the same result.
How can I host my node app on a local network and have everyone who is on the local network be able to access it through the browser?
EDIT: I'm running on CentOS 7.
EDIT2: When I run netstat -lnt, it says this:
tcp 0 0 0.0.0.0:3000 0.0.0.0:* LISTEN
Have you tried to just omit the IP address? It should then be available on the IP address of the machine it is running on and the specified port.
As suggested by HA. remove the IP.
As you can see from the documentation:
https://nodejs.org/api/http.html#http_server_listen_port_hostname_backlog_callback
If the hostname is omitted, the server will accept connections directed to any IPv4 address (INADDR_ANY).
P.S. Which is the OS on the server?
Maybe you can try :
app.listen(3000, '0.0.0.0', function(){
console.log("Node app running on 0.0.0.0:3000");
}
A possible issue could be you aren't using the http module?
var http = require('http').Server(app);
http.listen(3000, function () {
console.log('App running on port 3000');
});
A good practice would be set the port like
app.set('port', (3000));
var http = require('http').Server(app);
http.listen(app.get('port'), function () {
console.log('App running on port ' + app.get('port'));
});

Resources