Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS HTTP - listen on other port than 80

I am running XAMPP on Windows to host an Apache server on port 80. Now I'm trying to have a NodeJS script running in the background but the problem is that it can only listen on port 80. If it does, everything works as it should but I can't have Apache running at the same time, because Apache takes priority and just serves my website. The NodeJS script doesn't even get to listen.

My question is: how do I switch the listening port of the NodeJS script (the specific port really doesn't matter) so that Apache can still run on port 80 and I can reach the NodeJS script from all around the world.

Part of the NodeJS code:

const http = require('http');

const port = 8080;
const host = '0.0.0.0';




server = http.createServer( function(req, res) {
    if (req.method == 'POST') {
        var body = '';
        req.on('data', function (data) {
            body += data;
            doStuff(body);
        });
        res.writeHead(200, {'Content-Type': 'text'});
        res.end('received request successfully');
    }
    else {
        res.writeHead(405, {'Content-Type': 'text'});
        res.end('not allowed method ' + req.method + ', try again with GET or POST');
    }

})

server.listen(port, null, function(error){
  if(!!error){
    console.log("\x1b[41m%s\x1b[0m", "error while initializing listener on port " + port + ": " + error);
   }
   else{
    console.log("\x1b[32m%s\x1b[0m", "started listener at 'http://" + host + ':' + port + "'");}
 });

Additional information is in my other question which got flagged as duplicate.

like image 315
Sv443 Avatar asked Oct 22 '18 09:10

Sv443


1 Answers

Looking at your other question, which was marked as duplicate of this one, you've got some additional information there that will probably help to elucidate what you're needing. Specifically, you mention the following:

I want to host multiple http servers with NodeJS, that all get and send http requests. At the same time I want to have Apache running, which occupies port 80. If I disable Apache and let NodeJS run on port 80, it will work but I can't have them running at the same time.

This script will run and receive requests locally at port 8081, but I can't seem to send an AJAX request to it through the Internet, even after forwarding the port with my router:

I think @ankit-agarwal is probably right in that you need a reverse proxy setup to forward traffic to your different backends. Assuming you've got an externally facing IP address, you should be able to access each of your backends using the ports they are listening on. For example, if the exposed public IP address of your machine is 100.120.110.43:

+---------+------+-------------------------------------+
| Backend | Port |             Web Address             |
+=========+======+=====================================+
| Apache  |   80 | 100.120.110.43 or 100.120.110.43:80 |
| Node1   | 8080 | 100.120.110.43:8080                 |
| Node2   | 8081 | 100.120.110.43:8081                 |
+---------+------+-------------------------------------+

If you want to access each of the backends without specifying the port, you have to have some way to tell your internal network which backend to serve based on the request. One way of doing this is to use path based routing, where you setup your reverse proxy to route the traffic to the different backends based on the path in the url. You didn't post your Apache configuration, but you can use your current Apache server to handle this using the ProxyPass and ProxyPassReverse directives similar to below:

ProxyPass "/node1"  "http://100.120.110.43:8080/"
ProxyPassReverse "/node1"  "http://100.120.110.43:8080/"

ProxyPass "/node2"  "http://100.120.110.43:8081/"
ProxyPassReverse "/node2"  "http://100.120.110.43:8081/"

The cool thing about using a reverse proxy is that you don't have to expose your node backends to the public. Let's assume you haven't, and they are only accessible from the internal network at 0.0.0.0:port.

Listen 80
<VirtualHost *:80>
    DocumentRoot /www/apache
    ServerName www.apachefrontend.com
    ProxyRequests off
    ProxyPass /node1  http://0.0.0.0:8080/
    ProxyPassReverse /node1  http://0.0.0.0:8080/
    ProxyPass /node2  http://0.0.0.0:8081/
    ProxyPassReverse /node2  http://0.0.0.0:8081/
</VirtualHost>

You could also point to different hosts/ips that only you have access to.

Finally, you can also use VirtualHost and ServerName if you have different DNS records to point to the different backends.

Listen 80
<VirtualHost *:80>
    DocumentRoot /www/apache
    ServerName www.apachefrontend.com
</VirtualHost>
<VirtualHost *:80>
    ServerName www.nodebackend1.com
    ProxyRequests off
    <Proxy *>
        Order deny,allow
        Allow from all
    </Proxy>
    <Location />
        ProxyPass /  http://0.0.0.0:8080/
        ProxyPassReverse /  http://0.0.0.0:8080/
    </Location>
</VirtualHost>
<VirtualHost *:80>
    ServerName www.nodebackend2.com
    ProxyRequests off
    <Proxy *>
        Order deny,allow
        Allow from all
    </Proxy>
    <Location />
        ProxyPass /  http://0.0.0.0:8081/
        ProxyPassReverse /  http://0.0.0.0:8081/
    </Location>
</VirtualHost>

For any of the above to work, you need to have mod_proxy and mod_proxy_http enabled in apache.

These probably aren't the most robust examples and I haven't tested them, but they should demonstrate the idea. You can learn more here.

like image 173
willascend Avatar answered Oct 09 '22 18:10

willascend