Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot access node server on Mac from different device on local network

I'm running node server on my Mac with OS X 10.9.Here is the code

var http = require('http');

http.createServer(function (request, response) {
  response.writeHead(200, {'Content-Type': 'text/plain'});
  response.end('Hello World\n');
}).listen(3000, '0.0.0.0');

console.log('Server running at http://192.168.1.120:3000/');

I can use Safari on my mac to access server at http://192.168.1.120:3000/. But when I access the server with my iPhone, it failed. My Mac's firewall is off and my iPhone is at the same WiFi with Mac.

like image 368
Han Pengbo Avatar asked Jun 07 '14 07:06

Han Pengbo


1 Answers

Answering due to low reputation. I'm not sure if this addresses the issue you have.

First, I noticed the address in the listen function and what's printing out to the console are different. Second, if the Node is running on your computer locally, you would want to connect to your computer's IP, not the one provided by the Node server. I believe Mac has an ifconfig command for the terminal. This should give you back the computer's IP on your network.

I copied your code and removed the '0.0.0.0' from the listen function. I was then able to spin up the server and connect to it on my phone (using the computer's IP). Granted, this was on a MacBook running Arch and using a Note III to connect.

Hope this helps!

like image 140
shellhead Avatar answered Oct 19 '22 18:10

shellhead