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

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

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.

Using Node on Network Server

How does node work? I have installed node on the server. On the same network is my work computer where I have git bash and my project files. Am i unable to run my files on my work computer as long as I call the right port number? I am running the code below on my local machine calling the ip address and port number. But then I am getting this error. Error 0x2 starting node.exe index.js
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World!n');
}).listen(1337, 'ip');
console.log('Server running at http://ip:1337/');
Normally this is due to a corrupted binary, I would reinstall both git and node to fix problem.

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!

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