Nodejs application run without port - node.js

I've developed a few Node.js programs that operate on various ports.
Is there a way to use the paths http://localhost/node1 and http://localhost/node2 to run without port access?

If you want your app to be accessible over http protocol then your app needs to listen on a port.
If you just want to avoid using port when you type the url in the browser you need your app to listen on port 80 which is used by browser as default so can be omitted.

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.

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

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.

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.

Resources