node.js real time chat server - node.js

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

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

Publish NodeJS Server Online

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

How do ports work in Node.js when on the server?

I'm currently developing a node server locally, and to access it I have the server listen on 8080 and then go to localhost:8080 in the browser. However, how will ports work when I put this project on the internet?
How it works once you "put it on the internet" will be exactly the same. The browser still makes a request to a port (most likely 80 or 443 on the internet), except it will likely be going to a webserver via the internet instead of localhost.
If you want to learn about deploying a node project to a server check out this guide. Basically you need to setup a proxy to sit in front of node to handle all the server stuff like requests and ssl certificates.
I am assuming you mean serve your node.js program and proxy your localhost port to Internet by put this project on the internet. A short answer is that you never host your API on some port on Internet but exposure your local port to a Internet port.
I believe you already know someway to serve a node.js program stably. If not, I really recommend pm2.
If you want to exposure your API to Internet, you first have to get an IP for your local network so that other people can reach your network by this IP. You can also relate it to a domain. Then use softwares like IPfire to manage port mapping. For example, you can map yourIP:8080 to IP ofLocalhostAtLocalNetwork:8080. Then, people can reach your API through yourIP:8080. Or you can simply map yourIP:80 to IPofLocalhostAtLocalNetwork:8080 so that people can reach your API directly by your IP.
Another very popular tool to server and proxy services is nginx. With Nginx, you can easily serve some services and proxy whatever port to whatever service.
You will need to access the global variable "process.env" to dynamically determine the port number when your code is running from a server. The process.env property returns an object containing the user environment.
You may do something like the following to cater to both local and server environments dynamically:
//set port and ip
app.set("port", process.env.PORT || 8080);
app.set("ip", process.env.IP || "0.0.0.0");
app.listen(app.get("port"), app.get("ip"), function (err) {
if (err) {
console.error(err);
return;
} else {
console.info("cicak is running on port " + app.get("port") + ".");
}
});

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/

What is the best way to make a page only accessible through localhost?

Working on node app and want to make the contents of a page only available when on localhost. What is the best way/best practices to accomplish this?
put local ip restriction (eg 127.0.0.1)
refer to this stackoverflow link for more info on how to do it
Restrict access to Node.js-based HTTP server by IP address
If you are using Express, you can use this:
var express = require('express');
var app = express();
...
app.listen(3000, '127.0.0.1');
You can find more detailed explanation about this feature here: http://expressjs.com/en/api.html#app.listen
You can also achieve this with adding a rule in your firewall that will allow only connections from localhost.

Resources