Publish Node.JS server on the internet
Asked Answered
S

4

12

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

Send answered 12/1, 2013 at 12:36 Comment(0)
A
13

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.

Audra answered 12/1, 2013 at 12:39 Comment(2)
Do you think the router is baked in my machine (an iMac) or in my broadband Wifi box?Send
Worked like a treat. Thanks a bunch.Send
C
4
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).

Cacomistle answered 9/3, 2019 at 11:47 Comment(0)
G
3

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/

Graver answered 5/7, 2017 at 10:13 Comment(0)
G
0

Here's and easy option. As 2023 there's a new feature in VS Code, it's called Port Fowarding. I'm using a Angular application but It seems to work with any local port(It worked with the backend running spring in eclipse too!)

check if the ports are matching

You have to login with GitHub, and set the "Visibility" to public, then just access de "Fowarded Address" in any browser to test it. The browser may say it's unsafe though, in this case just add the link to ignore list.

Grenoble answered 10/10, 2023 at 13:57 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.