skip to Main Content

I have an angular universal app set up. I do POST requests on the server-side using localhost to pre-render my app and this works fine.

An example working url would be http://localhost:8000/api/get-info.

I’ve now put the app into production on an external url (apache server). I’m also using ssl.

Now when I try to do a POST request on the server-side to pre-render my app, I get back a response with status: 0, url: null (I’m assuming this means the connection was refused).

An example non-working url would be https://mywebsite.com/api/get-info.

What really stumps me is that when the app loads on the client, all HTTPS requests start working. So the problem is I cannot get the express server to send POST requests to my external url.

I’ve tested a post request on the server-side to a different website (twitter), and that seems to work fine as well. So i’m not entirely sure where I’ve gone wrong.

I already have CORS set to ‘*’ as well.

2

Answers


  1. Try using

    http://localhost:8000/api/get-info

    in production as well. Since your Angular app is rendered on the same server as your API is running, using localhost should just work fine. It doesn’t matter if you are on an external URL.

    I do something similar (its a GET but that shouldn’t matter) with my translations:

    if ( this.isServer ) {
         translateLoader.setUrl( 'http://localhost:4000/assets/localization/' );
     } else {
         translateLoader.setUrl( 'assets/localization/' );
     }
    

    It works locally and in production (both server and client).

    Login or Signup to reply.
  2. I just encountered this problem myself for two days. Please take a look at my comment on https://github.com/angular/universal/issues/856#issuecomment-426254727.

    Basically what I did was I did a conditional check in Angular to see if the APP is running in browser or in server (rendered by Angular Universal), and change my API endpoint to actual IP in https or localhost in http accordingly. Also in my Nginx setting, I only redirect incoming request from browser to https by checking if the server_name is localhost.

    Hope it helps!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search