Node webserver needs the port number - node.js

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)

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.

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

Hide the port when node.js is running

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.

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.

Resources