Access Node.js server by URL without port at the end - node.js

My server is running on a Node.js environment with Express. My server works fine, but I can't remove the port at the end of the domain name from the URL.
What is the right way to access my app with an URL without port at the end ?

Client side
By default, the port is 80 when a browser make an HTTP request.
If you type localhost, the real request is localhost:80 because no port is specified. It will be the same with any domain name. If you type example.com, the real request is example.com:80.
It is the client (here the browser) which choose on which port it will make his request to the server.
You can force your browser to emit a request on any port by adding :port_number after the domain name, as localhost:3000 or example.com:3000. Here we change the port from 80 to 3000.
Server side
The web server chooses on which port it listens for requests. It can be 80, 3000 or any other port.
If a client makes an HTTP request, your web server needs to listen to the right port. If the client emits example.com:4000, your web server must listen on port 4000 to get and process the request.
To make a web server, you can use Node.js, Apache (used in LAMP), Nginx etc. You can have multiple web servers running on your system and each of them can use multiple ports, but you can't make them listen on the same port. One of your web server may not start or could take the lead on others or crash...
Solutions are to use only one web server or to use multiple web server on different ports. In your situation, you are using LAMP so Apache web server. Its probably running on port 80 in his configuration. In this case you can't run a Node web server on port 80 because it's already in use. You should choose another port like 3000 for example. Both Node and Apache will then run on your system but on different ports respectively 3000 and 80.
In this last situation, you can access directly to Apache, but not to Node without precise the port 3000. To be able to access Node web server by port 80 without stopping Apache, you need to go through Apache and to make it redirect requests to your Node server in some cases. To do that, you need to configurate a proxy in your Apache. Note that it would be the same if you was using Nginx or other web servers.
Example
Let's take a simple express server on port 3000 :
// server.js
var express = require('express'),
app = express(),
http = require('http').createServer(app),
port = 3000;
app.get('*', function (req, res, next) { res.sendFile(__dirname + '/views/index.html'); });
http.listen(port, function () { console.log('App running & listening on port ' + port); });
If you type in the terminal node server.js, you can access from browser by localhost:3000, but you can't access by localhost because no web server is running on port 80.
If you change port variable to 80, you can access from browser by localhost or localhost:80, but not by localhost:3000 anymore.
If you edit /etc/hosts (sudo nano /etc/hosts) with a new line 127.0.0.1 example.com, you can access from browser by example.com if port is 80, else example.com:port_number like example.com:3000. This third solution maps domain name to ip address in your local client only.
If the chosen port, 80 for example, is already in use by another process (as LAMP), your node server may not works. In this case you should close this other process first or choose another port for your node process. In the third example, if you close the LAMP first, you can access from browser by example.com, if you choose another port for Node, you can access from browser by example.com:port_number like example.com:3000 for Node and still access your LAMP server on port 80.
Don't forget that 80 is the default port used by the browser if no port is specified. If you use another port, you should precise it from the browser by adding :port_number after your domain.
Now if you own a real domain name you will need to make a real DNS mapping not juts edit /etc/hosts. Configure your DNS on your registar account (where you bought your domain name) to make it point to your server's IP. Like that, when a client make an HTTP request to the domain name, it will be redirected to your server.
To have both Apache and Node.js running and available on port 80, you should make a proxy as explain above. Indeed, for you the problem is probably that you have a web server already running on port 80 (Apache with LAMP) and you want also your Node.js app to run on port 80 to don't force clients to precise the port at the end of the url. To fix that, you need to make a proxy in Apache conf to redirect requests which come from the specific domain name to your localhost node server process on the right port.
Something like that in your apache conf :
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
ProxyRequests Off
ProxyPreserveHost On
ProxyVia Full
ProxyPass / http://127.0.0.1:3000/
ProxyPassReverse / http://127.0.0.1:3000/
</VirtualHost>
Here when a request arrive on your server on port 80, Apache will check if it comes from example.com and if it is, it will redirect to 127.0.0.1:3000 where your node server will take the lead. The two different process (Apache & Node) should run in the same time on your server on different port.

If you want to run your node js server without any port and simply by http://localhost then listen your express js server on port 80 .

You could either do as stated by the previous answers and run on port 80 OR
you could keep the server running on whatever port you want and setup a proxy server such as nginx and forward the HTTP requests to said server.
This could be helpful in case you want to spin up multiple instances or even different processes.

When you see a URL, without a port, it means one of two ports are being served:
https:// - port 443
http:// - port 80
Even assuming the port is not in use, you can't service directly to port 80 without superuser privileges because port 80 and port 443 are privileged ports.
If you want to test the server running on port 80 directly:
sudo node index.js
Where index.js is the name of your Express application.
Keeping it running
Because you tagged apache, I'm assuming you want to know how to set up a node server using Apache. If you don't need a production quality server and just want to keep it running all the time, you can do that too.
Dev/Just keep it running
You can daemonize your server. A quick look for a "node" solution exposes forever as a way to do that. Simply install and run like this:
yarn global add forever
# or
# npm i -g forever
# remember, sudo for port 80
sudo forever start index.js
Production/Apache
Use a non-privileged port for Node, and set up a proxy in Apache. Something like:
ProxyPass / http://localhost:8000
If you set the port to 8000. Put that in a <VirtualHost>. Examples here. Likely you would still want to daemonize your nodejs Application using forever or some similar daemon tool (systemd is great for Linux services)

Related

What does the localhost: 3000 server created by express mean?

What is it that you access when you type in "localhost: 3000" onto your browser? What does that represent?
localhost is a special domain name that always refers to your local computer.
3000 is a tcp port. When you open a url such as:
http://localhost:3000/
Then a connection will get opened to port 3000.
When you specify a url like:
http://localhost/
Then you will open port 80, because the default HTTP port is 80
localhost:3000 is a local development server running on port 3000 of your computer. It is created by the express and can render html pages and route the apis for different requests. With the expressjs framework you can create different routes and associate it with the development or production port. You can also change the port during the development which can be done by changing the value of listening port on your app/index/server .js file.

Access NodeJS server installed on Linux server

I created my App from this boilerplate
https://github.com/Bikranshu/express-react-boilerplate
Now I uploaded it to a live Linux server and Node server is running.
Screenshot of running server
But I am unable to access it through Browser with IP address of server.
http://ip_address:3000
After waiting long in browser it is showing timeout error.
Please guide me how can I access the node/react app from browser.
Server running at <ipaddress> is a local IP, are you in a different network than the server? If so, you should be typing https://<public ipaddress>:3000
UPDATE
Hosting services usually only forward port 80 (http) or 443 (https.) This means that your port 3000 is not allowed for public access. To fix your problem you need to change the listening port.
Check line 42 on
server/app.js change 'port' to "80" or check package.json and edit npm start to set port to 80

Redirect all request internally to 3000 port apache2

I have a web application hosted on apache server. When I hit https://example.com then it points to the server but my application is running on 3000 port. So I want all the users to hit example.com (443 port) and internally it redirects all the request to 3000 port and make my application running.
You want to create a reverse proxy with Apache. These tutorial may help you:
https://linuxtogether.org/configuring-reverse-proxy-for-node-using-apache-mod-proxy/
http://garrows.com/blog/running-node-js-and-apache-together-using-mod_proxy/

Where do I put my Node JS app so it is accessible via the main website?

I've recently installed a nodejs app (keystone) app in my home/myusername/myappname directory.
When I visit www.mydomain.com, nothing displays - even after turning on my nodejs app.
Where should these files be?
I am running ubuntu 16.04.
In the past I have worked with a var/www folder, but I am not using apache - do I need to manually create this folder?
Thanks!
For your app to be visible it has to be running (obviously) and accessible on port 80 (if you want it to be available without adding a port number to the URL).
It doesn't matter where it is on the disk as long as it's running.
You don't need Apache or nginx or any other server. Your Node app may listen on port 80. But alternatively it can listen on some other port and your other server (Apache, nginx, etc.) can proxy the requests to that port.
But if your app is listening on, e.g. port 3000 then you should be able to access it as http://www.example.com:3000/.
Also, make sure that your domain is configured correctly. It's A record for IPv4 (or AAAA for IPv6) of the www subdomain should be equal to the publicly accessible IP address of your server.
And make sure that the port you use is not blocked by the firewall.
Update
To see how you can set the port with Keystone, see:
http://keystonejs.com/docs/configuration/#options-server
It can be either changed in the config or you can run your app with:
PORT=80 node yourApp.js
instead of:
node yourApp.js
but keep in mind that to use the port number below 1024 you will usually need the program to run as root (or add a special privilege which is more complicated).
It will also mean that this will be the only application that you can run on this server, even if you have more domain names.
If you don't want to run as root or you want to host more application, it is easiest to install nginx and proxy the requests. Such a configuration is called a "reverse proxy" - it's good to search for info and tutorials using that phrase.
The simplest nginx config would be something like this:
server {
listen 80;
server_name www.example.com;
location / {
proxy_pass http://localhost:3000;
}
}
You can set it in:
/etc/nginx/sites-available/default
or in a different file as e.g.:
/etc/nginx/sites-available/example
and then symlinked as /etc/nginx/sites-enabled/example
You need to restart nginx after changing the config.
You can find more options on configuring reverse proxies here:
https://www.nginx.com/resources/admin-guide/reverse-proxy/
You need to make a proxy between Apache and your Node.js application because Node.js has a built-in server. Supose your Node.js app is served on 9000 port. Then you need to make a proxy to redirect all trafic in 80 port to 9000 port where the Node.js app is running.
1. Enable mod_proxy
You can do this through a2enmond.
sudo a2enmod proxy
sudo a2enmod proxy_http
2. Set the proxy
Edit the /etc/apache2/sites-available/example.com.conf file and add the following lines:
ProxyRequests Off
Order deny, allow from All
ProxyPass / http://0.0.0.0:9000 ProxyPassReverse / http://0.0.0.0:9000
This basically say: "Redirect all traffic from root / to http://0.0.0.0:9000. The host 0.0.0.0:9000 is where your app is running.
Finally restart apache to enable changes.

Node.js and Apache: connection issues

I have installed Node.js with Socket.io on a CentOS server which is running Apache on port 80.
I created a socket test, which justs listens on port 8080.
If I curl the address localhost:8080 from within the server's shell, I get the Socket.io-welcome message. If I have a line like this:
<script src="http://localhost:8080/socket.io/socket.io.js"></script>
Then the browser cannot find the file.
A "solution" was to proxy requests to /nodejs/ to http://localhost:8080/, but this solution did not work for very long.
Is it possible to run the Node.js server when we have Apache installed? Which settings must be changed in order for us to access the url: http://server.com:8080 ? It seems the Node.js only accepts connections from localhost.
Problem is most probably in your node.js program.
It should listen on 0.0.0.0 and not 127.0.0.1 which is local only.
So where you've got something like:
.listen(8080, '127.0.0.1'); // '127.0.0.1' or 'localhost'
You should change it to:
.listen(8080); // or 0.0.0.0
Apache will only interfere if it also uses port 8080 but you should get an error when starting your node app if this is the case.
Also, if you connect to http://localhost in your browser, it will only work if the server is on the same local machine as the browser. Fine for testing I guess.
You'll have to connect to a domain or ip address if you have a hosted server else no browser will find it.
Update:
Your socket.io code also needs to connect correctly:
var socket = io.connect('http://correct.server.com:8080'); // not localhost
and your browser needs to load the javascript file from the correct place:
<script src="http://correct.server.com/socket.io/socket.io.js"></script> // not localhost
This might help with firewall / load balancer issues:
https://github.com/LearnBoost/socket.io/wiki/Socket.IO-and-firewall-software

Resources