significance of node.js port choice - node.js

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.

Related

Which is best port to use for secure Chat server to allow general firewall bypass (port 443 is already occupied)

so I developed a public chat application which will run on a node server using secure socket.io.
That server, which only has a single IP address already has ports 80 / 443 occupied.
So I need to find the next best port to use for the chat server.
I wonder is there a recommended next best port that will allow most firwalls to communicate to? I know for example using ports like 21 (FTP) will normally be blocked.
And is it best to pick one above 1024 or below?
thanks
Sean.
In general 8443 will be the "alternative port for HTTPS", but you are still at risk of being filtered.
The proper solution should be to run proxy like nginx on port 443 and provide access to various applications based on the hostname, not the port. In example you can configure it to run your current app when user reaches https://example.com and chat app when user reaches https://chat.example.com.
Here is an example article showing how to do it https://www.manuelkruisz.com/blog/posts/nginx-multiple-domains-one-server
The idea is that each app runs on different internal port on the server, and proxy running on port 443 picks which app the request should be routed to based on the hostname.

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.

Run node js application without port

I wants to run my application without assigning any port on my current IP address on port 80. How it will possible.
If your node.js application is a web server, you cannot remove the port. No port, no web server. Trying to make a web server without a port is like trying to make a locomotive with no railroad.
You can use the default port however. When users give browsers URLs without ports, they automatically apply the default port. For URLs like http://example.com/ or http://10.11.12.13/ the default port is 80. For https://example.com it's 443, and you need to use the https server class.
So, you can make your server listen on port 80.
In development you will run into a problem with this approach. On OSX, Linux, and other UNIX-derived OSs, only the privileged (root) user can run servers that use port numbers less than 1024. The typical development cycle of edit / run / test is a huge hassle, and a security hole, when you need privileges to run. That's why the node.js examples use port 3000.
In production, many people use nginx as a reverse proxy server to relay http requests from port 80 or https requests from port 443 to your node.js server at port 3000. You can read about how to do that; it's far beyond the scope of a Stack Overflow answer,

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.

Accessing Websites through a Different Port? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
I am wanting to access a website from a different port than 80 or 8080. Is this possible? I just want to view the website but through a different port. I do not have a router. I know this can be done because I have a browser that accessing websites through different ports, Called XB Browser by Xero Bank.
Thanks for the answers. So, if I setup a proxy on one computer, I could have it go from my computer, to another computer that then returns the website to me. Would this bypass logging software?
A simple way is to got to http://websitename.com:174, and you will be entering through a different port.
If your question is about IIS(or other server) configuration - yes, it's possible. All you need is to create ports mapping under your Default Site or Virtual Directory and assign specific ports to the site you need. For example it is sometimes very useful for web services, when default port is assigned to some UI front-end and you want to assign service to the same address but with different port.
You can use ssh to forward ports onto somewhere else.
If you have two computers, one you browse from, and one which is free to access websites, and is not logged (ie. you own it and it's sitting at home), then you can set up a tunnel between them to forward http traffic over.
For example, I connect to my home computer from work using ssh, with port forwarding, like this:
ssh -L 22222:<target_website>:80 <home_computer>
Then I can point my browser to
http://localhost:22222/
And this request will be forwarded over ssh. Since the work computer is first contacting the home computer, and then contacting the target website, it will be hard to log.
However, this is all getting into 'how to bypass web proxies' and the like, and I suggest you create a new question asking what exactly you want to do.
Ie. "How do I bypass web proxies to avoid my traffic being logged?"
No, as the server decides what port it is run on. Perhaps you could install a proxy, which would redirect the port, but in the end the connection would be made on port 80 from your machine.
You can run the web server on any port. 80 is just convention as are 8080 (web server on unprivileged port) and 443 (web server + ssl). However if you're looking to see some web site by pointing your browser to a different port you're probably out of luck. Unless the web server is being run on that port explicitly you'll just get an error message.
It depends.
The web server on the other end will be set to a certain port, usually 80 and will only accept requests on that specific port. Something along the chain will need to be talking to port 80 to the website.
If you control the website, then you can change the port, or get it to accept requests on multiple ports.
If the website is already talking on a different port, you can just use the colon syntax to reference another port (eg: http://server.com:1234 for port 1234).
If you want to use a different port on your client end, but you want to talk to port 80 at the web server end, you'll need to route traffic from port x to port 80. A common way to get this up and running is to use Port Fowarding. ssh can do this for you, see here for a Unix/technical overview or here if you're on Windows.
Hope that helps.
when viewing a website it gets assigned a random port, it will always come from port 80 (usually always, unless the server admin has changed the port) there's no way for someone to change that port unless you have control of the server.
If website server is listening to a different port, then yes, simply use http://address:port/
If server is not listening to a different port, then obviously you cannot.
Unless you're browsing through a proxy, the web servers hosting the sites you want to access must be configured to listen to a port other than 80 or 8080.
Perhaps this is obvious, but FWIW this will only work if the web server is serving requests for that website on the alternate port. It's not at all uncommon for a webserver to only serve a site on port 80.
You can only access a website throught the port that is bind with the http server.
Example: i hava a web server and it is listening for connections on port 123, the you only can get my pages connecting to my 123 port.
To clarify earlier answers, the HTTP protocol is 'registered' with port 80, and HTTP over SSL (aka HTTPS) is registered with port 443.
Well known port numbers are documented by IANA.
If you mean "bypass logging software" on the web server, no. It will see the traffic coming from you through the proxy system's IP address, at least. If you're trying to circumvent controls put into place by your IT department, then you need to rethink this. If your IT department blocks traffic to port 80, 8080 or 443 anywhere outbound, there is a reason. Ask your IT director. If you need access to these ports outbound from your local workstation to do your job, make your case with them.
Installing a proxy server, or using a free proxy service, may be a violation of company policies and could put your employment at risk.

Resources