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

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

Related

Nodejs hello world

Hi guys I was hoping for a little help , My overall goal is to make an web scraper running from an server. But I figured I would have to learn how to walk before trying to run ...
So I have simplified my problem to this:
How to get response in browser window for simple hello world app?
This is what I have done
Created an server at digital ocean.
setup the server.
Setup Putty and fileZilla
So far all of this work. But how can I get this little code to work:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(3000, '178.62.253.206');
console.log('Server running at http://178.62.253.206:3000/');
When I run this from the PuTTY terminal I get this response in the console window:
- Server running at http://178.62.253.206:3000/
But when I navigate to this in chrome browser I get this error:
Failed to connect to 178.62.253.206 port 3000: Connection refused
What Im I doing wrong ?
Any help at all would be much appreciated
Frederik
You're only accepting connections from 178.62.253.206. Change this:
}).listen(3000, '178.62.253.206');
To this:
}).listen(3000);

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!

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.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