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

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.

Related

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

Can't Access Node.JS over internet

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.

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

Custom Port not working for node.js app on AWS EC2 instance

I have deployed the following code on my AWS EC2 instance -
const express = require('express')
const app = express()
app.get('/test',(req,res) => {res.send('Hi')})
app.listen(3001, () => console.log('Server running on port 80'))
When I try to visit the following url - http://ec2-13-59-209-0.us-east-2.compute.amazonaws.com/test , I get connection refused message. The message on the UI is ec2-13-59-209-0.us-east-2.compute.amazonaws.com refused to connect.
I did go through the documentation and set up security group to listen on port 3001. But that did not help either.So I enabled on traffic for all the ports. Still I was not able to connect. Please find below snapshot of the security group. It would be great if you can help me with this.
You need to tell Express to listen to all traffic, not just localhost traffic. Change your app.listen line to:
app.listen(3001, "0.0.0.0");

Express - public server

Use Ubuntu
Have Express app with Hello world. I wanna run app as public, setup static ip like in this video. It is my express server app:
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.send('Hello World!');
});
var listener = app.listen(3000, function () {
console.log('Example app listening on port 3000!');
console.log(listener.address());// { address: '::', family: 'IPv6', port: 3000 }
});
I check my inet address with ifconfig command and get 192.168.0.102 - it is local IP address and when I enter 192.168.0.102:3000 I get Hello world. But when I try 194.242.103.139:3000 didn't have success. I am new with this stuff, please help and if you can with more details.
Thanks.
Assuming 194.242.103.139 is your public IP address, make sure you have enabled route (port forwarding) on your router/gateway/firewall. Or put your machine IP to DMZ.
Important If you don't know what DMZ or port forwarding or anything mentioned above is, or what security risks this brings, please, don't mess up with your router. You would be asking for a nasty surprise. Read something on the topic of networking and security first.

Resources