NodeJS [SOLVED]: apache2 / nodejs / express on a digital ocean server – how to listen to public port (80) without address in use error

NodeJS [SOLVED]: apache2 / nodejs / express on a digital ocean server – how to listen to public port (80) without address in use error

Home Forums Scripting NodeJS Tutorials NodeJS [SOLVED]: apache2 / nodejs / express on a digital ocean server – how to listen to public port (80) without address in use error

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #245863

    Cloudy Point
    Keymaster

    QuestionQuestion

    I’m new to nodejs in a server environment, and trying to figure out how to make it work in conjunction with all of the other things I have running on my digital ocean server.

    I was successfully able to set up a sandbox environment on a subdomain on digital ocean in which I have an express server listening for requests from port 3000. Then, from a web browser on port 3000, I could make an ajax GET request to the node server, and retrieve a response.

    However, now I am trying to implement that sandbox envionrment into a new subdomain, where I am running the craft PHP CMS. This runs off the default port of 80. So I want my node server to be able to listen to requests from port 80. But where before I could say:

    app.listen(3000, function () {
      console.log('listening')
    })
    

    If I were to try to run craft off of Port 3000, it would throw an error for the URL routing: Cannot GET [url_segments]

    because it is designed to look for requests from port 80, the default web port. And if I change the express server to listen to port 80, it gives an error saying the address is in use.

    > node app.js:
    Error: listen EADDRINUSE :::80
    

    How do I get app.js to just listen to the default public html port on my digital ocean server without this EADDRINUSE error? I want people to visit my subdomain:

    http://subdomain.domain.com/episodes/episode-2

    and have it be able to make calls via AJAX to my express server:

    var urlParams = new URLSearchParams(window.location.search);
    var startDate = urlParams.get('start');
    var endDate = urlParams.get('end');
    var urlWithParams = "/list?start=" + startDate + "&end=" + endDate;
    
    $.ajax({
        method: "GET",
        url: urlWithParams,
    }).done(function (data) {
        //do stuff
    })
    

    where the list function on my express server is something like:

    let app = express()
    
    app.get('/list', (request, response) => {
      //DO STUFF
    }
    

    I am on a digital ocean server using Ubuntu 14.04 and Apache2

    #245864

    Cloudy Point
    Keymaster

    Accepted AnswerAnswer

    You need to use a proxy.

    nginx example:

    server {
        listen 80;
        server_name subdomain.domain.com
    
        location / {
            proxy_pass http://localhost:3000;
        }   
    }
    

    Apache:

    <VirtualHost *:*>
        ProxyPass / http://0.0.0.0:3000/
        ProxyPassReverse / http://0.0.0.0:3000/
        ServerName localhost
    </VirtualHost>
    

    More info here

    Source: https://stackoverflow.com/questions/48029292/apache2-nodejs-express-on-a-digital-ocean-server-how-to-listen-to-public-p
    Author: Jonathan Dion
    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.