What is different between localhost(127.0.0.1) and remote server ip on node web server - node.js

I try to know web server based Node.js with ubuntu server. I set "localhost" in host variable at first, however, it's not working when I access remote server such as http://192.168.0.11:3000. so, I changed variable data to "192.168.0.11". It's working on browser.
I checked ubuntu firewall, port number, telnet on my local desktop. Could I know how it work on IP addr.
var http = require('http');
const host = '192.168.0.11';
// const host = 'localhost'; or '127.0.0.1';
http.createServer(function(req, res){
res.writeHead(200, {'Content-Type':'text/html'});
res.end('Hello world!');
}).listen(3000, host);
console.log('Server is running at',host,':3000');

Related

Access NodeJS demo application in AWS Lightsail outside Localhost

I am test driving AWS Lightsail for one of my NodeJS experiments. Installed NodeJS using SSH on the server and ran a demo program(see below). I can see the output "Hello World" from the SSH terminal with the command " curl localhost:3000". But when I access it from outside the network, using my web browser in my home PC, with the Public IP address and port number 3000, it says "This site can't be reached" I've forwarded the port 3000 on the server side.
Anything I am missing?
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(3000, "127.0.0.1");
console.log('Server running at http://127.0.0.1:3000/');
If you don't pass on host to .listen, server will run on all interfaces including 0.0.0.0. But you are running server to listen on localhost only.Change .listen like this:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(3000);
You will also have to add Inbound traffic rule(Firewall) to open port 3000 to access the server over the internet as your server running on port 3000.
Click on an instance and then select networking option
Go to firewall section and click on "add another" as shown in below pic
Click on save and then you should be able to access a server with http://IP:3000.
If you want to access your server without specifying a port in URL then you have to configure 'Nginx reverse proxy'. Also, port 80(for http) and port 443(https) should be allowed through a firewall.
If you .listen(3000, "127.0.0.1"); listen on 127.0.0.1, it only allows local machine to access the server. You need .listen(3000, "0.0.0.0"); to allow any ip address to access the server.

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

node - what's wrong with my app?

I've got httpd running on port 80 and I'm trying to bind a node app to port 8080.
Here it is:
var server = require('http').createServer(function(req, res){
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
});
server.listen(8080);
Obviously have run it from ssh command line via
node myApp.js
But whenever I type "http://my-domain:8080/" in the browser it just hangs and gives me nothing..
I've tried a range of different ports and listening on hostname 0.0.0.0, all giving the same result.
Have run netstat as comments suggested and results is :
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 3894/node
I'm using centOS on nan unmanaged VPS!
EDIT: Looks like its a firewall issue, could someone point me in the right direction as to how to configure the firewalls for a CentOS VPS.. ?
Might be your firewall settings. In the shell prompt on the server, try connecting with curl.
curl -v http://localhost:8080/
If you can access it via the localhost but not via a browser then you most likely have a firewall issue.
If you can access the server via localhost then the next thing to do is to test the server from the outside via IP address. If you can access it via IP address then you have a DNS issue. If you cannot access it via IP address from the outside then you have firewall issue.
Firewall issues are platform specific. We'd need to know the platform to point you in the right direction.
try to write:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(8080);
console.log('Server running ');
the .listen(8080); is in the same line.
or to run it with localhost:8080/ from the server may he block from outside

Node.js on MAC: Access a Node.js web server from another computer

I built a Node.js web server on my computer, using the so-well-known-http-web-server-example of Node.js:
var http = require('http');
http.createServer(function(req, res){
res.writeHead(200, {'content-type': 'text/plain'});
res.end('It works');
}).listen(3000, '127.0.0.1');
This works (as expected) on the computer that runs the server.
I would like to access the server from another computer, in the same LAN. Using ifconfig on the terminal of the computer that runs the server (Apple MacOSX), I get: 192.168.0.6.
So, in my other computer, I opened my browser and connected to http://192.168.0.6:3000, but I get:
Oops! Google Chrome could not connect to 192.168.0.6:3000
My final aim, is to be able to connect to the server using my smartphone.
Any help would be welcome. Don't hesitate to ask for more details if necessary.
Thanks in advance :)
127.0.0.1 is only local interface. Try to start listening all interfaces:
var http = require('http');
http.createServer(function(req, res){
res.writeHead(200, {'content-type': 'text/plain'});
res.end('It works');
}).listen(3000, '0.0.0.0');

node.js server not responding

i am trying this simple demo off of the node.js home page:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
i have opened the port on amazon ec2 (1337) in its security group.
[root#domU-12-31-38-01-8A-8D servers]# /usr/local/bin/node nodeexample.js
Server running at http://127.0.0.1:1337/
i get nothing back but the typical server is not responding. please help this noob out
danke
You're listening to 127.0.0.1 which makes node listen only to the loopback interface and only lets it be be reached from localhost itself.
If you listen to 0.0.0.0 instead, you will listen to all the machine's network interfaces and lets you be reachable over the Internet on any public IP the machine is using. This is probably what you want.

Resources