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.
Related
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
currently my hosting uses a new firewall, and according to their plan, they don't allow any http connection based on ipv6 and all connections should use ipv4.
I have a service using node.js and expressJs, and I also use pm2 as a process manager to run my application, my problem is that http requests failed due to using ipv6. How could I force node.js to listen to version 4 IP address on nodeJs app.
The part of my code which I listen to a port:
const app = express();
...MANY MIDDLEWARE app.use();
mongoose.connect(MONGODB_URI, {useNewUrlParser: true, useUnifiedTopology: true})
.then(result => {
app.listen(APP_PORT);
socketServer.listen(SOCKET_PORT, function () {
console.log('server listening to: %j', socketServer.address())
});
})
.catch(err => {
console.log
});
Can I use something like below with express:
var server = http.createServer(app).listen(APP_PORT, APP_IP);
I was curious about it too, but based on a useful link it worked like a charm :
https://github.com/nodejs/node/issues/18041.
I had this block of code :
var server = http.createServer(app);
server.listen(port);
In order to listen on ipv4, I made the following change :
var server = http.createServer(app);
server.listen(port,"127.0.0.1");
Giving the try one more time the port was listening with ipv4, otherwise it is listening on ipv6 :
$ sudo netstat -lntp |grep -i 3000
tcp 0 0 127.0.0.1:3000 0.0.0.0:* LISTEN 20501/node /home/ml
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.
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
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'));
});