Express.JS server to connect to host on remote network - node.js

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

Related

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}`);
}

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

pointing node.js app to my ip address and running locally

So I have two questions about the topic. I have purchased a VPS with digitalocean.com to host my node.js app and they gave me an example hello world app to start.
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
var html = "hello world";
res.end(html);
}).listen(8080, 'localhost');
console.log('Server running at http://localhost:8080/');
So this code works locally and when I host on the VPS I replace 'localhost' with the IP address of my VPS and it is available on the web when I go to http://IP_Address_of_VPS:8080
My first question: How do I host it live on the web but have it point to my IP address? When I replace 'localhost' with my IP address it does not work when I go to http://my_IP_Address:8080
My second question:
I made a test app following a node.js tutorial and here is the code
var PORT = process.env.PORT || 8080;
var express = require('express');
var app = express();
var http = require('http').Server(app);
app.use(express.static(__dirname + '/public'));
http.listen(PORT, function(){
console.log('listening on port *:'+PORT);
});
Where public is the folder with a simple html page. This app works when I run it locally at http://localhost:8080
Can someone explain how the second app runs locally when I did not specify 'localhost' in the http.listen() function? Also how do I point the second app to run from my IP address if the process is any different from the first?
First question
I hope I understood your question right, here is an attempt at an answer :)
Depends a bit on what you mean with 'my ip address'. If it is your public ip adress, you might have to set up port forwarding. Most routers will deny traffic from the outside network (which you are doing when connecting to your own public ip address) to the internal network by default.
If you mean a private ip address (local to the local network) you need to you use your public ip address and set up port fowarding.
When you set up port forwarding to your local machine you might also want to make your private ip address static.
However, I do not recommend using your home computer as a production server. You will need to solve a lot of problems like making your network secure and having low downtime.
Second question
When no hostname is passed to http.listen, the server will accept connections on all adresses. See documentation for more information.
This works, because when you omit arguments in a function call, they will default toundefined. For example, http.listen(8080) might appear as http.listen(8080, undefined, undefined, undefined). Inside the function body these will often be substituted with some default value.
The process of making your app available to the rest of the world should not be any different.

Redirect localhost url in node.js and express

i have a project where users connect to my router, and then enter the address http://192.168.1.50:9091 into the address bar to connect to a page. I would like there to be a way to enter something easier than this long ip address.
here is what's happening so far:
var express = require('express');
var app = express()
, http = require('http')
, server = http.createServer(app)
, io = require('socket.io').listen(server)
, fs = require('fs')
server.listen(9093);
var nRequest = 0;
var nConnexs = 0;
app.get('/', function (req, res) {
res.sendfile(__dirname + '/client_app.html');
});
app.get('/2', function (req, res) { res.sendfile(__dirname +
'/client_app2.html'); });
I am not sure what you mean by:
i have a project where users connect to my router
The Node.js application is sitting on your machine. Until somebody accesses your machine (through IP), the application is never accessed.
As such, you must put something between the user and the application that will direct the user to the IP. For real websites, this is accomplished using DNS. You register a domain name and tell the DNS service the IP of the machine to redirect to.
If you have control over the router (internal network, etc.), you can map an alias to your IP address. It all depends on whether your router runs a DNS server or supports DNSMasq. You will need to check your router manufacturer/model/etc.
Finally, if there are a small number of users that will be accessing your website, you could always have them use the hosts file to map a name to the IP. The location of this file is dependent upon the operating system; just Google: edit hosts file
I assume users are initially connecting to your router's public ip address or a DNS representation of that. If that's the case, you can usually configure your router to do port forwarding such that incoming requests on a particular port are automatically routed to a particular private IP address on your network. In that case, the users would connect to your router, but that request would be automatically forwarded to your internal node server and thus the users would end up directly connected to your node server.
This type of configuration comes with the usual security warnings. Doing this means that your node server must be properly configured against internet attack and your node app must be written with appropriately security precautions - particularly because any compromise to the computer the node server is running on has access to your internal network.
If you show us what users are initially connecting to when they connect to your router, we could offer a more explicit example of how you could configure the router to take advantage of port forwarding.

Allow Public IP to connect via WebSockets

I tried to creating a chat with nodejs ws (einaros),this is my code:
Server:
var WebSocketServer = require('ws').Server;
var wss = new WebSocketServer({ port: 80 });
wss.on('connection', function(ws) {
console.log('connecting count:' + wss.clients.length);
});
Client:
var hostname = location.hostname;
var port = 80;
var url = 'ws://'+hostname+':'+port+'/';
window.WebSocket = window.WebSocket || window.MozWebSocket;
var w = new WebSocket(url);
when I test with my computer or some other computers who connect with my router, it works well. However, other clients who visit via Internet cannot make the connection. Of course, they use the public IP, like 218.xxx.xxx.xxx, not 192.168.xxx.xxx.
I wonder how to solve this problem.
Thank you for you answer. But, it may be not the NAT problem. For example, My public IP is 218.100.50.50,and My private Ip is 192.168.1.1. When a connection comes from outside my network and visits 218.100.50.50:80, the router will redirect it to 192.168.1.1:80. There's a web page which can be visited in this way, but still cannot make the connection to my websocket server. This problem really confuses me much.
Sitting behind router with IPv4 leads to problems for routing actual traffic to right direction (computer behind router).
You need to Forward port 80 in router settings to your computer IP. Additionally I would recommend set in router settings preferred IP for your computer so after restart it is more likely not to change local IP address (otherwise can happen).
After those changes, you can even try to connect using Public IP from your own computer, and if it will work - then it should work from external computers.
Additionally check firewall settings to make sure it does not blocks any external traffic to port 80.
Your problem is called NAT: http://en.wikipedia.org/wiki/Network_address_translation
When a connection comes from outside your network (that is, against your public IP), the router doesn't know who's going to receive that connection. Imagine there are 3 PCs and 2 smartphones connected to your router, which one is going to "answer"? Well, what you have to do is tell the router: "dear router, when my friends try to connect on port 80, redirect them to device 192.168.1.37 and port 80 (for example)".
And if you don't want to reconfigure your router again and again to update 192.168.1.37 (because of DHCP random IP assignation) then tell your router to assign the same IP to your PC always (static ip).
So, configure a "static IP" for your PC, and create a port forwarding rule for port 80 to redirect traffic to your server. You can search "server behind router configuration" for more details, there are thousands of tutorials.

Resources