Hide the port when node.js is running - node.js

how can I hide the port when I ran my node application
example
http://localhost:3000/application/login
What I need is to hide port 3000 so that it stays that way
http://localhost/application/login

You need to run the server on port 80, but to do this, you need root privileges, and it used to be a bad idea.

Phrasing it a bit differently.
When you open a http:// url, browsers will by default use port 80. Only if you specify a port, browsers will try a different port instead.
So if you don't want the port to appear in the url, you must use port 80.

Related

How can I remove the port from the url?

I want to hide my port number in NodeJS,
for example:- I was running on a port 4001,if I want to make a request for '/xyz' webhook I need to make a request as 'https://example.com:4001/xyz' in this I am not interested to show my PORT to others I want to mask or hide it from the public it should be as "https://example.com/xyz"
Please help me to setup as above
I'm assuming your using localhost to serve your app. Once you deploy to the web, the port number will be handled automatically as all http requests are routed to port 80 by default.
May be you have given port number in your code. just remove that, instead keep process.env.PORT or process.env.PORT || 4001. so that the server will take port 80 by default.
now you no need to add the port number while visiting the url.
tldr switch your nodejs app to run on port 80.
The existing answers are all correct but none explain the why. The HTTP protocol (your site is a http server) uses port 80 as a default, yet you are running your server on port 4001 so it needs to be explicitly stated. If you go to http://example.com you are actually making the http request on port 80, it just doesn't need to be explicitly stated as that is the assumed default. There is no difference between http://example.com:80 and http://example.com.
That being said there is no security need to "mask" or "hide" your port. If you switch it to 80 it isn't hidden it just doesn't need to be typed. The only reason to make this switch is because it is easier, shorter, makes your site look more professional ect.

Hosting Nodejs application without port

I have a nodejs application running on port 3000. I wanted to host it on Linux environment. So I installed nodejs in it. It's working fine but I should specify the port each time.
example: mydomain.net:3000/url_i_want,
How can I avoid this. and also when running my app like that, all users are kind of connected to each others. If one of them disconnect all other users are. If one of them change page all others have there pages changing. Is it because they are all listening to the same port 3000 ? I searched and found that it can be related to PM2 and Nginx. Is it the solution ?
Whenever you load a URL without specifying the port number, the browser defaults to 80, because 80 is the default port number for HTTP.
So if you load http://stackoverflow.com/questions, the browser "converts" it to http://stackoverflow.com:80/questions.
If you don't want a port number to be specified to access your website, your app should be listening on port 80, instead of 3000.
However, it is not recommended for Node apps to directly listen on port 80 (although they very well can).
What you can do is use a front-facing proxy such as nginx, which accepts connections to the host's port 80, and then redirects the request to localhost:3000, where your app is listening.
It is best to ask one question at a time.
As for your second question, unless you are using some sort of "remote syncing" framework, that sort of behavior is unexpected. I would suggest posting a separate question for that issue with more details about it.

Node webserver needs the port number

this is probably a simple thing so I hope you will be kind with your answers even if the question is not so clever.
I set up a webserver and everything seems to be running fine. The only problem I have, is that I need to give the port number instead of just the IP when I want to open a website. It works fine like this http://xxx.xxx.xxx.xxx:8080 but doesn't without the 8080.
The problem is I don't even know what to look after. If you need the output of anything or more information, please let me know and I post it.
Thanks!!!
Your server is listening on pot 8080, when there is no port on the URL, that means that the server is listening on port 80 (or 443 for HTTPS).
You should read the documentation for your server and see where the setting for the listening port resides and change the 8080 into 80.
For example, in a random script.js file using net or http modules:
server.listen(8080); // Requires that you add :8080 to the URL
server.listen(80); // Doesn't requires that you add :80 to the URL,
// if you add it it will be removed by your browser.
You need to bind your server to port 80 for http. To do this you had to run your server with root privileges and this is really not reccommended. You can redirect your traffic from port 80 to port 8000 using iptables.
Read here: Best practices when running Node.js with port 80 (Ubuntu / Linode)

significance of node.js port choice

I am a bit new to server side scipts. I am fairly capable with javascript so I decided to take a look at node.js as opposed to php or python ect. Correct me if I am wrong but it seems that when I code my webserver I may freely choose the port number I listen to. Is there any significance to this port number or may I choose any random number I wish? Will anyone be able to send a request to my server regardless of the number I choose?
Thanks in advance!
If you want to run node.js directly without any supporting web server or reverse proxy (no nginx, varnish, apache, etc), you need to listen on port 80 for HTTP and (optionally) 443 for HTTPS if you want normal URLs to work. Otherwise users will need to type the port number in the URL like http://example.com:3000 which is unheard of for public-facing sites.
However, you almost certain DO want to use a separate web server as I describe in detail here, in which case any port over 1024 is fine.
If you have root access you can choose any port that's not already bound to a process.
If you do not have root access you can choose any port above 1024 that is not already bound to a process.
Port 80 is usually the one you want to use if you're serving up HTTP, however, you can access an HTTP server on any port via the URL port syntax. For example, a server on port 3000. http://yourdomain.com:3000
If you're running on Linux and you do not want to run your Node process as root, you can redirect port 80 traffic to another port.

Node.js port compatibility

I'm starting an app in node.js, using socket.io and it's on the same server that runs apache, so port 80 is unavailable. By default, it's using port 8080. I read on socket.io's site that port 843 is generally not blocked.
I understand this is also the port used for flash files. Is there reason not to use port 843? or likewise, not to use port 8080?
Also, would the ideal solution be to use a different server and run on port 80?
Is there reason not to use port 843? or likewise, not to use port 8080?
Yes, I know some offices block all ports but the common ones (80, 21, 25, etc). If you're just testing node, playing around, or even during development, then it doesn't matter.
Also, would the ideal solution be to use a different server and run on port 80?
Yep.
Just run on port 8080, especially if you only use socket.io and not the complete node.js stack. Makes no difference if you ask me.

Resources