How to bind http server with express object to a specific ip address? - node.js

How do i bind a specific address to a http object? Currently the default ip address for node.js is 127.0.0.1 and I want to change it to 0.0.0.0
var express = require('express');
var app = express();
var http = require('http').Server(app);
http.listen(3000, "0.0.0.0");
How to achieve that?
edited: I added 0.0.0.0 but the default is still 127.0.0.1

0.0.0.0 is not an actual IP that you can reach. Although it means bind to all IPs, or any IP. So no wonder it works for 127.0.0.1. If you have set an IP address on one of your network devices (LAN, WiFi, virtual) it'll listen on those too.
In python you can simply type runserver 0.0.0.0 or something, so in Node.js is there an alternative?
process.argv gives you a list of arguments passed to node.
So if you run
node server.js 0.0.0.0
You'll get
process.argv[0] //=> "node"
process.argv[1] //=> "server.js"
process.argv[2] //=> "0.0.0.0"
Note that it's an array, so as long as you're sure you'll be running your file like that you can use process.argv[2] to specify that as the IP address you want to listen to.
http.listen(3000, process.argv[2]);
Generally though, you should set environment variable IP.
http.listen(3000, process.env[IP]);
Or all of them
http.listen(3000, process.argv[2] || process.env[IP] || "0.0.0.0");

Related

AWS public ip with port not working for nodejs

I have installed nodejs in aws ubuntu 18.04 version. Added port 3000 in security Group. but node js not working in my public address with port for eg: http://3.xx.xx.xx:3000.
Note: pm2 running with node js
The solution for this is :-
in your backend inside app.js listen on port 3000 or which ever port you wish to.
Go to amazon console, got to security groups associated with your ec2 instance, and under incoming connection add a custom tcp rule with port 3000 and source should be 0.0.0.0/0 and save it
Run netstat -pan | grep 3000 if you noticed that node.js listen on 127.0.0.1 only not 0.0.0.0 so 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.
For example:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(3000, "0.0.0.0");
console.log('Server running at http://0.0.0.0:3000/');
last step if you did not already, go to the Security Groups tab in the EC2 console. Right click the security group you setup and click edit inbound rules.
Click Add Rule. This time we are going to use a custom TCP rule on port 3000, open to anywhere.

Node server not reachable from outside

I am playing with a small node server application.I have hosted it in AWS Lightsail's ubuntu instance. It is reachable from local browser like http://localhost:4201/
but when I try to access it from remote, it is unreachable.
In aws instance's network config I have opened traffic for all ports
I have cleared all rules from iptables as well. I am able to reach http port 80 and ping successfully. But no luck with node server, what am I missing? Is there a special way to enable traffic to node server?
I debugged it, basically in server.ts
I had bound it to localhost so it won't accept request from outside
app.listen(4201, '127.0.0.1', function () {
console.log('Server Listening on 0.0.0.0:4201');
});
changed it to 0.0.0.0
app.listen(4201, '0.0.0.0', function () {
console.log('Server Listening on 0.0.0.0:4201');
});
And now it is accessible from everywhere.

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.

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'));
});

Publish Node.JS server on the internet

I have a Node.JS server that works fine on localhost. Now I want it accessible from the internet, hosted by my machine. My public IP address (the one that Google tells me I have) does not seem to be "accessible":
https.createServer({
key: privateKey,
cert: certificate
}, server).listen(80, '86.151.23.17');
fails with the following Node.JS error:
Error: listen EADDRNOTAVAIL
at errnoException (net.js:770:11)
at Server._listen2 (net.js:893:19)
at listen (net.js:937:10)
at Server.listen (net.js:994:9)
at dns.js:71:18
at process.startup.processNextTick.process._tickCallback (node.js:244:9)
How can I publish my Node.JS server to my public IP address?
[Note: I do not have another webserver running. Also, I have tried various different ports as suggested here.]
You are most likely behind a router so your public IP is not available anywhere but on the router itself. What you need to do is listening on your private IP (usually somehing in the 192.168.* range) and setup a port forward on your router.
In case you are on Linux you'll also want to use a port >1024 instead of 80 so you don't have to run node as root. When setting up the port forwarding you can simply forward port 80 to whatever port your node server is running on.
const http = require("http");
const hostname = '0.0.0.0';
const port = 80;
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
using 0.0.0.0 will start listing to the public internet I have tested it.
I have experienced the cases that the ISP given router is intercepting default 80 and 443 ports. Even though the ports are opened. So better check server first using a port like 8080 etc.
And also configure port forwarding to a static local address (ipconfig /all assumed your host is windows) then assigned that IP address to your host using host's MAC address.
for a better experience, if you don't have a static IP, use noip.com dynamic domain names to access your server at any time (without knowing IP address).
Your app should listen on other ip address, example
app.listen(3000,'0.0.0.0');
or just
app.listen(3000);
Then you must open port forwarding in your modem. Like this http://www.dlink.com/uk/en/support/faq/routers/wireless-routers/dkt-series/how-do-i-open-up-ports-to-my-computer-port-forwarding-on-this-router
Finally you can see your app at ip address in here https://whatismyipaddress.com/

Resources