Significance of port 3000 in Express apps - node.js

I noticed that almost all examples of Express.js applications use port 3000 as the default listening port for HTTP servers. Is this just because it's a rarely used port, or is there any other reason for this port number?
If I want to run multiple apps side-by-side on my local machine, is it good practice to use ports like 3000, 3001, 3002, etc.?
(I understand that ideally, you'd let the system assign ports. This is just a question as a matter of simplicity, and why 3000 seems to be a conventional assignment.)

3000 is a somewhat arbitrary port number chosen because it allows you to experiment with express without root access (elevated privilege). Ports 80 and 443 are the default HTTP and HTTPS ports but they require elevated privilege in most environments.
Using port 3000 in examples also helps indirectly emphasize that you ideally want to put your express app behind nginx or Apache httpd or something like that which would listen on port 80 and/or 443.
There is no reason (that I'm aware of, anyway) why 3000 is better than 8000 or 4000 or 8080 or any of a number of other port numbers that are accessible without elevated privileges.

Related

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,

express-ws: How to avoid typing the port on the request url?

I've successfully created a WS Server using Node.js and wxpress-ws. The problem is that I don't know which port to use to not type it in the request URL. So, instead of using ws://mysite.com::xxxx/, I need to be able to type only ws://mysite.com/
I've tried to listen port 80, 8080, without success.
If your application is listening on port XXXX then you will need to access it via indicating the port in your url ws://myapp.com:XXXX/.
If you want to use ws://myapp.com/, you will need to listen to the port 80, two solutions for you :
Launch your application with sudo node myapp.js privileges and make it listen on port 80. I don't recommend this approach since it might introduce some vulnerabilities because the app is running with admin privileges.
Set up some reverse proxy like nginx that will listen on port 80 and that will redirect the traffic to your application listening on port XXXX. You can find a lot of tutorials online

What the differences between node ports and what is best to choose?

One tutorial uses port 3000. Other uses 8080 while someone just uses 8000 or other ports.
What all of those mean and why should I use 3000 instead of 8080 or vice versa?
Thanks.
Ports are only endpoints for communicate one application with another.
There are some ports uses by default depending of the protocol, for example 80 for HTTP.
There is no restriction with it, you can use the HTTP protocol over port 443 if you want, but some applications (like web browsers) use default port numbers if you don't explicitily set another one.
The number of available ports are from 0 to 65535, there are some conventions but you can use anyone you want.
Ports like 3000 or 8080 are typical values when you are developing an application (it is not a rule, but a convention).

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)

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