Reach Nodejs express app with external ip - node.js

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");

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

How to connect to node.js server from any ip

I want to create a private backend for an application I want to make, but I am having trouble connecting to my node server, I have the basic stuff right now,
var http = require("http");
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('<html><body><h1>Hello World</h1></body></html>');
}).listen(3000);
console.log('Server running on port 300.')
But this only works for https://localhost:3000/, how do I make it so that if I have a separate computer on a separate connection, I can connect to this server?
I am not interested in just opening it to everyone but just to specific client IP's...
If the client IP's are on the same network as you, you can check out this question
If you want people from anywhere to access your application, I suggest hosting it on something like Heroku (very easy to deploy, pretty good free tier). You can then create a whitelist of IPs in your express application.
I would suggest for any port forwarding using ngrok or configuration in your router
For downloading ngrok https://ngrok.com/ go to this link
For configuration your router it will take some searching in google based on what type of router your using
You must mention that your localhost or Nat Ip and your public IP to resolve here is NOIP refrence https://www.noip.com/support/knowledgebase/general-port-forwarding-guide/
As you specified that you want the backend to be private, such that it can only be accessed by your specified node. You will have to host this node server on a cloud service or you can host it on your local machine by opening a port for the node server. Suppose you host the node server on port 1234 on your local machine ip address.
You can start the node server on localhost and your desired port, but you need to allow requests to the particular port.
Now you need to check for the origin of the request that your node server receives and validate that, so that only your private node(computer) can access the node server. You can do that by validating the hostname using express, you can get the hostname of the request in express.js using req.hostname or req.headers.host.
You would need to use express.js for this functionality and the code would be as follows
let express = require('express');
let app = express();
let allowedHost = 134.32.234.3 // the hostname which is allowed to access the backend
let port = 1234; // desired port
let host = 0.0.0.0 // desired host; 0.0.0.0 to host on your ip
app.get((req, res) => {
res.header('Content-Type', 'text/html');
if(req.hostname == allowedHost){
res.send('<html><body><h1>Hello World</h1></body></html>');
}
else{
res.send('Connection not allowed');
}
});
app.listen(host, port, ()=>{
console.log(`The server is running on http://${host}:${port}`);
}

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.

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");

Resources