node.js server not responding - node.js

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.

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.

NodeJS server only works on hosting machine, not on other devices

I have set up a Node.js server on my computer, using the following code:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, "0.0.0.0");
console.log('Server running');
Now, when I use this computer to go to http://localhost:1337/, http://127.0.0.1:1337/ or http://192.168.178.28:1337/ (192.168.178.28 being my IPv4 address), it works fine.
When i try and use my iPad to visit the last one (ipv4 address) it doesn't work. Why could this be?
Okay, turns out resetting my router did the job. Thanks to lazlojuly and Gonzalo Bahamondez for trying to help!

connecting to node.js http server on linux machine from windows machine

I'm trying to write my first node http server. I have got it running on my linux box. If I type the url in the browser. I see the hello world webpage.
myLinuxHostName:1227/
I'm now trying to connect to this linux node server from my windows machine. If I type my in the browser from my windows machine the same url I get webpage not available. I tried pinging my linux host and that worked fine. What am I doing wrong?
I'm using the simple http server code that is there on nodejs.org homepage.
If you are using this example:
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/');
Then only runs using this exactly ip 127.0.0.1 whats mean localhost and the other VHost not reach that server. You must doing something like this.
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337);
For more information: http://nodejs.org/api/net.html#net_server_listen_port_host_backlog_callback

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

Resources