简体   繁体   中英

External Hello world server node js

I am looking to use Node.js to host a web server on a dedicated PC, but I cant seem to access it from anywhere besides my local network.

From what Ive found online, it seems like all I have to do is enter the externalIp:port in a browser on a different network and I should see my Hello World, but I cant get it to work without exposing my localhost through something like ngrok.

Does anyone know how could I access my node server from an external pc on the internet and not just localhost?

Here are the steps to reproduce

require("http").createServer(function(request, response){
  response.writeHeader(200, {"Content-Type": "text/plain"});
  response.write("Hello World!");
  response.end();
}).listen(8080);
  1. "node server.js" - very simple server hosting on port 8080 that just sends 'Hello World!' response
  2. check localhost:8080 on my machine, see "Hello World!" working
  3. get externalIP from ipchicken.com
  4. check 'externalIP':8080 on external machine (phone, diff network pc), never works

Maybe there is something I am missing, but I thought this was pretty straightforward

So it turns out the router I was using from Optimum has a "special" settings page to allow Port 80 traffic. And there is no link to it anywhere on the port forwarding page. So after doing all your port forwarding, make sure you follow this link:

http://optimumdev.custhelp.com/app/answers/detail/a_id/2140/related/1

You need to bind the service to all available ip interfaces, change your listen 8080 to :

require("http").createServer(function(request, response){
   response.writeHeader(200, {"Content-Type": "text/plain"});
   response.write("Hello World!");
   response.end(); 
   }).listen(8080, "0.0.0.0")

Assuming you have a standard home network setup, you can't just send a request to your external IP address and expect it to magically reach your machine. The request will hit your router and the router won't know what to do with it. You need to configure your router to forward requests on port 8080 to your local address (probably something like 192.168.0.x). Even that may not be enough. You may need to configure a software firewall on your machine to allow incoming requests on that port as well. Hope this helps :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM