0

I am using the MEAN Stack and I could run my frontend already. After ng build I moved all the content in the dist folder to var/www/html and could access my website. I'm using Apache and my frontend is now available online. The problem is now my backend. I am using Node.js but I have no clue how I can make it "online" for everyone. With npm start server.js or pm2 start server.js I can make my Node.js server run and everything is working fine, but it's only working for me. When my friend accessed my website, the backend is not running (only frontend).

So my question is, how can I make my node.js server public? Is there a way to run node.js in Apache also?

Actually I made a apache proxy and even with nginx but seems not to work yet...

I followed this step from here: https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-14-04

and also from here: https://medium.com/@pierangelo1982/using-nodejs-app-with-apache-374b37c6140d

EDIT:

I am able to server my backend with nginx but I have to stop my Apache server but my apache server is serving the angular frontend...

What should I do?

EDIT2:

Thanks of the help from Gouveia, I am able to serve front and backend with NGINX

server {
    listen 80;

    server_name http://travelinked.vm.mi.hdm-stuttgart.de;

    location / {
        root /var/www/html;
    }

    location /api {
        proxy_pass http://141.62.65.110:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

As you can see the site is available but still the backend is not running outside of my intranet. For me the backend is loading but for you guys I think not

http://travelinked.vm.mi.hdm-stuttgart.de is the website

1 Answer 1

1

When you run npm start server.js, you are running a server on its own.

If you want all requests to go through Apache, then you can use simple reverse proxy to connect from Apache to your nodejs backend (assuming your node js server runs on port 8080):

ProxyPass "/api"  "http://localhost:8080/"

Apache config files in linux are usually located here: /etc/apache2/sites-available/.

If you want to use Nginx instead, you can serve your static files with it, while proxying the requests to the API as well:

server {

    location / {
        # Path to your angular dist folder
        root /path/to/static/files;
    }

    location /api {
        # Address of your nodejs server
        proxy_pass http://localhost:8080/;
    }
}

The config file in linux is usually located here: /etc/nginx/nginx.conf.

With both approaches, accessing your Apache/Nginx server on the /api/* path will make a request to the nodejs server.

In Nginx case, the path /api is included in the request path to nodejs server. To remove that part of the path, you will need a rewrite rule:

location /api {
    # Address of your nodejs server
    proxy_pass http://localhost:8080/;
    # This removes the /api part in the request to the backend
    rewrite /api/(.*) /$1 break;
}

I've used localhost in the examples, as I'm assuming you are running everything in the same machine.

For more details and configurations, I would advice to check the linked documentation.

5
  • Hey thanks! I editted my post! The backend is still just running locally even though with NGINX, am I still missing something?
    – Takuya2412
    Commented May 15, 2021 at 21:28
  • 1
    Your frontend code seems to be pointing to the IP address of the backend (141.62.65.110:3000/gettoptours) instead of http://travelinked.vm.mi.hdm-stuttgart.de/api/gettoptours.
    – F. Gouveia
    Commented May 15, 2021 at 21:37
  • Hey it's me again, for some reason it cannot get /api when I access http://travelinked.vm.mi.hdm-stuttgart.de/api Hello World should be there but instead I get Cannot GET /api
    – Takuya2412
    Commented May 15, 2021 at 22:02
  • 1
    I'm not sure at the moment, but you might need to either change the path of your routes in the node js code to include /api or change the nginx config to rewrite the path. See serverfault.com/questions/379675/… According to the answer, it would be something like this in your case: rewrite /api/(.*) /$1 break;
    – F. Gouveia
    Commented May 15, 2021 at 22:10
  • Rewriting the path did help! Now it finally works! Thank you so much.
    – Takuya2412
    Commented May 15, 2021 at 22:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.