Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Publish Node.JS server on the internet

I have a Node.JS server that works fine on localhost. Now I want it accessible from the internet, hosted by my machine. My public IP address (the one that Google tells me I have) does not seem to be "accessible":

https.createServer({
    key: privateKey,
    cert: certificate
}, server).listen(80, '86.151.23.17');

fails with the following Node.JS error:

Error: listen EADDRNOTAVAIL
    at errnoException (net.js:770:11)
    at Server._listen2 (net.js:893:19)
    at listen (net.js:937:10)
    at Server.listen (net.js:994:9)
    at dns.js:71:18
    at process.startup.processNextTick.process._tickCallback (node.js:244:9)

How can I publish my Node.JS server to my public IP address?

[Note: I do not have another webserver running. Also, I have tried various different ports as suggested here.]

like image 478
Randomblue Avatar asked Jan 12 '13 12:01

Randomblue


3 Answers

You are most likely behind a router so your public IP is not available anywhere but on the router itself. What you need to do is listening on your private IP (usually somehing in the 192.168.* range) and setup a port forward on your router.

In case you are on Linux you'll also want to use a port >1024 instead of 80 so you don't have to run node as root. When setting up the port forwarding you can simply forward port 80 to whatever port your node server is running on.

like image 97
ThiefMaster Avatar answered Oct 07 '22 00:10

ThiefMaster


const http = require("http");
const hostname = '0.0.0.0';
const port = 80;

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

using 0.0.0.0 will start listing to the public internet I have tested it.

I have experienced the cases that the ISP given router is intercepting default 80 and 443 ports. Even though the ports are opened. So better check server first using a port like 8080 etc.

And also configure port forwarding to a static local address (ipconfig /all assumed your host is windows) then assigned that IP address to your host using host's MAC address.

for a better experience, if you don't have a static IP, use noip.com dynamic domain names to access your server at any time (without knowing IP address).

like image 43
Stan Avatar answered Oct 07 '22 01:10

Stan


Your app should listen on other ip address, example

app.listen(3000,'0.0.0.0');

or just

app.listen(3000);

Then you must open port forwarding in your modem. Like this http://www.dlink.com/uk/en/support/faq/routers/wireless-routers/dkt-series/how-do-i-open-up-ports-to-my-computer-port-forwarding-on-this-router

Finally you can see your app at ip address in here https://whatismyipaddress.com/

like image 31
Anh Nguyễn Tuấn Avatar answered Oct 07 '22 00:10

Anh Nguyễn Tuấn