Can't Access Node.JS over internet - node.js

I'm trying to access a simple node.js/express application over the internet, but I can't for the life of me figure out why it's not working. I can access it using http://localhost:3000 and http://192.168.x.x:3000 but not using my external IP address.
Port 3000 is open on my router (double checked with port online port checker tools), and I've added a rule in the firewall to allow the port (Windows 10).
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => res.send('Hello World!'))
app.listen(port, "0.0.0.0")
netstat seems to suggest that port 3000 is allowed through the firewall, right?
C:\WINDOWS\system32>netstat -n -a
Active Connections
Proto Local Address Foreign Address State
TCP 0.0.0.0:3000 0.0.0.0:0 LISTENING

You need to forward that port or make a tunnel to use it over internet . By default the ports are blocked. The problem is not the server is not listening the problem is that it is not discoverable from the outside.
To forward the port, Add the forward rule to your router [More info]
For tunnelling you can use ngrok
By doing that you can connect to the server at http://<your_external_ip>:<forwarded_port>

I finally figured out the issue! It has to do with me testing the connection inside of the LAN on a router that doesn't support hairpinning (see point 2 in this stackoverflow answer). Simply accessing the application on a device outside of my LAN does the trick.

Related

Serving an Express js app to public internet from my personal laptop

I have a Node express Js App running in my local Network accessible at port 80.
Here is the express.cjs app
const express = require('express')
const app = express()
const port = 80
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
Navigating to localhost:80/ I can see 'Hello World!' as expected.
I wanted to take this app to the internet serving it from my personal Laptop
Over the internet, I found port mapping will help me achieve this goal.
The following Steps were taken so far.
Port Forwarding.
Navigated to the default gateway address to enable port forwarding in the router. i.e 192.168.10.1
Router: WavLink
I added an entry to port forward incoming traffic as shown.
Uninstalled McAfee Antivirus Software.
Next, Navigated to Windows Defender Firewall with Advanced Security 
Created a new inbound port rule for port 80 to make sure that Windows Defender is not blocking the port.
Result:-
Then in my browser, I navigated to
http://61.68.**.**:80/   ( 61.68.**.** being my public IP ) 
I'm getting a page, as shown above, I'm not sure where I'm going wrong. Any pointers towards solving this issue will be very helpful.

Express.JS server to connect to host on remote network

So I got this small express server running. I can connect it to other devices on my local network e.g. mobile and other PC.
However when connecting over my 4g it does not work. Is there any reason for this? I am sure when I ping other private addresses on remote networks it has worked before, why not now?
Code:
const express = require("express");
const server = express();
const PORT = 3000
server.use(express.static("static"));
server.get("/", (req,res)=>{
res.sendFile(__dirname + "/pages/index.html")
});
server.listen(PORT, "0.0.0.0", (req,res) => {
console.log("Listening on port ", PORT)
});
Any information would be apricated I have some networking experience (still a noob just studying) and this really does interest me.
I assume you are trying to reach the server via your local IP. But you are doing it with 4G (in your phone maybe), which means your request is going over the internet while your local IP is only valid in your network.
Even if you are using your public IP, you would probably have to configure port forwarding on your router for it to know how to handle the incoming traffic for this port.
If you host your express server in your private network , you must have access to the express server by create port forwarding, destination nat, or some kind of publishing private services to public world methods.
If you need more help , i need to know more about your network env , your public ip and ...
Feel free to ask
An have a productive day

Making my Node app accessible to the Internet from a local machine

OK so I know I can use cloud hosting and I've done so before but I am doing a demo and I want my node app to be on my local machine but accessible from the internet. Here is how I start the server in the server file
const port = 8080;
var server = http.createServer(app).listen(port, () => {
console.log(`Server listening on port ${port}`);
});
Next I set up port forwarding on my Xfinity gateway such that both port 80 and port 8080 point to my desktop. I know I am connecting to the right device because SSH works from outside of my network on port 22. However when I enter [public IP]:8080 I am unable to receive a response. The only time I receive a response is when I enter 10.0.0.58:8080 which refers to my internal network. Why is this???
I personally like ngrok to do the same thing. It's really easy to setup and I found it really stable.
Give it a go https://ngrok.com
After installing you can simply forward ports like
ngrok http 8080

Reach Nodejs express app with external ip

So I create a NodeJs express app which I can reach by using my local ip-address (192.168.x.x), but when I use my ISP provided Ipv4 address I got "connection timed out".
The things that I done to make it live:
I disable firewall on ubuntu 16.04.
Open port at my router.
Even I put my local ip-address in DMZ.
If you have any suggestion please let me know!
My very simple code is here:
const app = require('express')();
app.get('/health-check', (req, res) => res.sendStatus(200));
app.listen(25565, "0.0.0.0");

Express: How do you get the hostname and port that an express server is listening on, WITHOUT waiting for a request

It's fairly straight forward to get the hostname and URL from a request that has been made to an express server. But there seems to be no obvious way to determine which URL an express server is listening on (after it has been started up, but before any requests have been sent).
How do you find out which host and port an express server is listening on? Does express even know which host and port it is listening on?
If you create an server like this:
var server = http.createServer(app);
The server instance actually contains the port and address it's listening. So you could just export it, and call server.address() to get the address and port.
{ address: '::', family: 'IPv6', port: 3200 }
If you want further info about what urls are your routes are routing, try
console.log(router.stack)
then the magic happens. :)

Resources