Publish NodeJS Server Online - node.js

I'm currently having an issue when trying to port forward my modem to my Node.js server. Sorry if this is a duplicate question, i've spent some time looking at familiar topics.
I'm using the express framework, I've included a small snippet, so you can see what port the node.js server is listening too.
(Quick Question: Do I use server.listen or app.listen in this situation? What's the difference?)
var express = require('express');
var app = express();
var server = require('http').Server(app);
app.listen(4000);
I've also attatched a picture containing:
The node.js server running.
ipconfig (so you can see my local IP address)
and a screenshot of my modems' port forward settings.
When I google "Whats my IP Address" and retrieve my public IP address, then navigate to it I receive an error on my browser stating that the server took to long to respond. I assume the server isn't being port forwarded, however I can't seem to isolate the issue.
Any help would be greatly appreciated?

Your modem/wifi router's public IP address keeps changing frequently every now and then or when your router reboots. This is because of DHCP.
Port forwarding and hacking your wifi router firewall rules to allow public access to your home or office network is an unsafe practice.
That's where simple tools like SocketXP come in handy. SocketXP will provide you a public URL to access your NodeJS server running in your localhost machine like your home/office laptop or server.
Here is a great blog article on how to install and setup SocketXP to access your local NodeJS server from the internet.

If you're wrapping your Express app in a http server, you should listen on it like so server.listen(4000).
Also make sure that your firewall on your computer is not blocking access to that port. A good alternative to the port forwarding + disabling firewall rules is to use a reverse proxy, something like ngrok for example

Related

How can I connect node.js file to my real website (not localhost)?

I am studying node.js and on the localhost I can perfectly do the below exercise but When I try to do it on my real website it fails. After hours of search, I am not sure what I am missing. Anyhelp will be really appreciated.
var http = require("http");
var fs = require("fs");
var server = http.createServer(function(req, res){
res.writeHead(200, {"Content-type":"text/html"});
if (req.url === "/contact") {
var myReadStream = fs.createReadStream(__dirname+"/contact.html", "utf8");
myReadStream.pipe(res);
};
});
server.listen("https://mywebsite.com");
More on setup
I have 2 files, one is contact.html, it's simple html page with "hello world". And the other file is myExpress.js file which contains above code and nothing else. When I do the above exercise on localhost, I am changing server.listen(3000, "127.0.0.1") and it works perfectly, but I do it on my website it doesnt work. I am using filezilla for loading files.
Your question is extremely broad - but, I will mention few high-level steps to take in order to run your script for real Internet traffic.
You need a server that faces the real Internet. It should be capable of running node.js. This can be rented from a hosting provider.
You need a domain name. You can obtain one from a Domain Registrar. Then you need to point your domain to your server via DNS settings.
You need to place your script in the server (hoping it faces the Internet properly) and listen to port 80.
Your port needs to be different in your server.listen. Change it to the one provided by your hoster - please provide some more info.
Example:
server.listen(3000) Listens on port 3000 on localhost
server.listen(process.env.PORT) Listens to the port - can be used for a hoster like heroku
HTTP, and HTTPS server are different protocols, so you need extra steps to create it.
Also server.listen accepts address and port, not protocol.
And finally: exposing raw Node app on production isn't a good idea (e.g. closing connections, queue manage). Recommend way is using some reverse proxy in Nginx/Apache and some monitors e.g. pm2 / forever, or more advanced: PusshionPassenger

node.js real time chat server

hey guys i made chat for my chat application on my local host with some guids i found on the internet and im struggling to add is to my real server on line, now it listening to port localhost:3000 but i dont know what sould i do for a real server, help me please!
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
server.listen(process.env.PORT || 3000);
app.get('/',function(req,res){
res.sendFile(__dirname + '/index.html');
});
Now you're gonna need to set up your server. I recommend digitalocean for a cheap cloud VPS. You can follow this tutorial.
https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-16-04
Basically you'll need to set up a process manager. For node i recommend PS2.
If you're looking to host your chat server from your residence, you will need to route external node traffic to the machine (or VM) which runs your node server.
You can do this by port forwarding. That means you'll need to go into your router's UI/settings panel and create a new port forwarding rule. Namely, you will probably want external traffic which arrives on port 80 to route to the local IP of your node server's machine on port 3000.
Once this is done and the changes have been saved to your router config, you should already be able to put your public IP in a browser's URL bar and be directed to your web-facing node app.
Beyond the scope of this question is getting yourself a domain name, and attaching it to your public IP, either statically or dynamically depending on your needs. That way, you wouldn't have to enter your public IP in the URL bar, you would just have to enter your domain, i.e. myfirstchatapp.com

How can I access my Express Node.js server through the internet?

I was able to successfully access my node server through a local IP address (192.168.XX.XX) on my WiFi network with the following server code:
var express = require('express');
var app = express();
var server = require('http').createServer(app);
server.listen(8080, "0.0.0.0");
I went to 192.168.XX.XX:8080 and accessed the HTML file successfully.
Specifically, I'm using my phone to access the server but if I get off of the network and use cell service, I can't access the server. I looked up my external IP and got 76.XXX.XXX.XXX. When I go to 76.XXX.XXX.XXX:8080 from my phone (without changing any server code), I can't get to the server. I also tried switching to port 80 in the code and it still didn't work.
I have a McAfee firewall going and I tried to open 8080 to be externally accessible. I also turned the firewall completely off but still couldn't connect.
Any ideas?
The seemingly best solution is using Ngrok. It allows you to expose your local server to the internet. You can download and use it here: https://ngrok.com/

Access private http server

I built a Nodejs HTTP server. It's running on localhost:3000.
From outside the Local Area Network, how does one make a request to the private HTTP server? Messing with the router manually is not an option; process should be automated.
I've looked at various techniques - and I'm confused:
Mapping the localhost port to the external ip address (node-nat-pmp)
HTTP tunneling (node-tunnel)
SOCKS (shadowsocks-nodejs)
CONNECT method in request header
It seems that everything is built for a client tunneling out through a firewall, I want to tunnel a request in through the firewall and to a private server. Or just run the localhost port on the external ip.
Any help would be appreciated. Confused.
If you need to access an internal service from outside of your network, you typically have two options:
Configure Port Forwarding on your router - You mentioned this isn't a possibility
Use UPnP to ask your router to open a port for you - This is often disabled as it is a security risk, but if not, look into https://github.com/TooTallNate/node-nat-pmp

Port Forwarding on website

I'm noob in this field, so please help me understand this:
I have my web application launched on port 8080. I thought that if i forward port 8080 and enter from web browser: myexternalipadress:8080/Index.html it should open my website. please tell me why i'm wrong
Ahh, hosting a website from your house is a fun thing indeed. In my experience, here is how I approach trouble shooting.
1.Making sure your server is configured properly.
Launch the server application
If your client and server are the same machine, make sure you can reach the server on localhost
Access the server from a different computer on your LAN, use your servers lan ip. 192.168.?.?:8080
If you can't get to it from another machine on your LAN, you may have firewall issues on the server
2.Making sure your network is configured properly
This is where port forwarding comes into play. Figure out the LAN ip of the server and log into the router.
Tell your router to forward the port (8080 in this case) to the server LAN ip address.
Test it by telling your friend to access your server on (WAN_IP:8080 in web browser)

Resources