Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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 :)

like image 782
htaidirt Avatar asked Jan 22 '13 22:01

htaidirt


People also ask

How do I access NodeJS server from another computer?

Given that the port is bind to any IP address other than 127.0. 0.1 (localhost), you can access it from any other system. To view your IP addresses, use ipconfig (Windows) or ifconfig (Linux) command. Find out the IP which is in the same network as the "other system" from which you want access.

How can I access my localhost website from another computer?

Put the IP addresses of both of your computers' internet security antivirus network security as safe IP addresses if required. On your PC you type in: http://localhost/ inside your Firefox or Internet Eplorer browser to access your data on your webserver.

Can NodeJS run on shared hosting?

You can run node. js server on a typical shared hosting with Linux, Apache and PHP (LAMP).


1 Answers

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');
like image 53
Vadim Baryshev Avatar answered Oct 02 '22 02:10

Vadim Baryshev