Nodejs server only listens on localhost - node.js

I created two virtual machines - One is Windows 7 and the other is RedHat 6.4. I am trying to run the simplest server on the RedHat machine and then access it from the Windows 7 machine. The problem is that I can't seem to make it work!
This is my code (Simplest there is) :
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(8080);
Tried to listen(8080,"0.0.0.0" and "127.0.0.1"). Both didn't work.
When I try to access with from the RedHat machine with "wget" command it works, but when I try to go to the Windows 7 machine and enter from chrome or even with "telnet IP 8080" it doesn't work.
Both machines have access to the internet and I can also ping from each to other meaning there is connection between the two.
Also I added the port to the iptables in RedHat and I think I allowed it in the firewall aswell. Just added a new rule that allows all ports.
I wanted to add that I also made a Windows Server 2008 machine and I tried to run the node server there and connect to it from my Windows 7 machine, But still the same problem exactly! Can connect from the Windows Server machine but not from any other machine.

Related

I can't run my node application in my on prem VM

I'm trying to run a node js application on an on-prem VM which is running RHEL 7. I'm not experienced in RHEL 7 and can't seem to find any details in running Node JS apps on it.
My app is super simple. Is a server which returns a message...
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Welcome Node.js');
}).listen(8000);
console.log('Server running on http://ip:8000');
I run the application and try accessing the IP with the port in the browser within the correct network. Am I missing something? My assumption was RHEL7 was the server and would show me the application once I open it on the browser.

My NodeJS server is not working in remove server

When I run my NodeJS server in remote server with script "next" for example, it compiles successful and get "Running in localhost:3000" but when I try to enter to ip:3000 didn't get response from server.
What I should do?
Ubuntu server.
The server respond correctly without :3000
Have you tried to open the ports on your server for port 3000? :)
I'm on Ubuntu so this may be different on your system, but i think this should work for you.
ufw allow 3000
If you want to access remotely you cannot listen your server using localhost
Try to listen server in 0.0.0.0
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");
Debugging this might follow a path on these lines:
Verify the server is actually running on that machine. Try to connect to it locally from another shell on that that remote/server machine (use curl if the machine is headless). If it's not running locally, you clearly won't be able to connect remotely.
Assuming the service is actually running, determine if you have the right IP address for the remote machine. Running something like ifconfig (ipconfig on windows I think?) will tell you this.
Determine if you can reach the machine at all over the network. You might try to send it a ping, or connect to another service you know it's offering. If you can't reach it, is it running a firewall that's preventing you or is there address translation going on somewhere?

Access server running on ubuntu host from guest in virtualbox

I have a nodejs project running in ubuntu. so I access it this way:
http://localhost:9000/login
now I am trying to access this server from a guest windows7 which I am running on virtual box.
My ubuntu IP is 192.168.1.13 and my VM network configuration is Bridged Adapter so I am trying
http://192.168.1.13:9000/login
But it does not work
However, when I run in the cmd 'ping 192.168.1.13' It replies successfully.
Could someone tell me what else I have to do to access my server from the guest?
For what you want to achieve, your Node.js application should listen on the specified IP address and port which is accessible from other systems.
You just can't expect the user from outside world(OS) to access your web-application which is running in your system's localhost.
Change your code to something like this for allowing it to listen on APP_PRIVATE_IP_ADDRESS(substitute your IP, which is 192.168.1.13 in this case) :
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
// your code
}).listen(8080, 'APP_PRIVATE_IP_ADDRESS');
console.log('Server running at http://APP_PRIVATE_IP_ADDRESS:8080/');
Also, for further accessibility, check How To Set Up a Node.js Application for Production on Ubuntu 14.04.
Look at file /etc/hosts and check how localhost is mapped. Surely it is set to 127.0.0.1 . If you make your nodejs application listen on IP:port 192.168.1.13:9000 you will be able to connect. Or change the mapping of localhost which I don't recommend.

Running a node.js server on my VPS on port 3000 and the connection times out

In hostgator I have a VPS running centOS. I installed NodeJS and screen.
I added the following code to a file named index.js:
//1
var http = require('http');
//2
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('<html><body><h1>Hello World</h1></body></html>');
}).listen(3000);
console.log('Server running on port 3000.');
On 'screen:1' I run the following command:
node index.js
It gives me the console output stating 'Server running on port 3000.'
I switch to 'screen:0' and run the following command:
curl localhost:3000
and I get the following response:
<html><body><h1>Hello World</h1></body></html>
Yet, when I try my server's IP address (substitute the xxx for a real IP address, cause I'm not disclosing my VPS IP address):
xxx.xxx.xxx.xxx:3000
The page never comes up and eventually it times out.
I've tried various ports (8080, 7000) and to not avail.
Do I need to place the iOS project in a different directory.
Currently I have it in /root/Projects/NodeTutorial2/index.js.
What do I need to do to get a hello world response from my VPS?
If you're getting a response from on the box, but not from other boxes, it's almost certainly a firewall issue. Turning off IPTables or allowing the traffic in on the port in question is one option but an easier / more appropriate option is to simply have your app use port 80 (for HTTP) or 443 (for HTTPS). You can either do that by listening to that port on the app directly, or by having a web server that acts as a reverse-proxy for you (e.g. NGINX or Apache).

Can't access node.js script from web browser

I installed node.js and socket.io in my CentOS 6.4 server. It is running parallel with Apache, with Apache as main server (port 80)
I wrote a simple Hello world script to test node.js installation:
var http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.write('Hello World\n');
response.end();
}).listen(8088);
console.log('Server started');
If I run it in command line I get 'Server started'. But when I tryh to access it via web browser, typing http://xxx.xxx.xxx.xxx:8088 it never loads. I've tried to use many other port numbers with no success. I have to ips in my server, but neither of them work, nor my domain addres under those ips.
How could I fix it?
EDIT: node,js is installed in another server, and I'm trying to access it via webbrowser from outside this server.
Thanks!
i think you need to open port 8088 by firewall on server.
see man iptables

Resources