Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Port and Proxy Config on ng-build

I have a frontend running for example on int.myapp.com and it's backend on int.backend.myapp.com. I thought I can do the proxing with the proxy.config.json like this:

 "/api": {
    "target": "https://int.backend.myapp.com",
    "secure": true,
    "changeOrigin": true
  }

And in my package.json file

"start": "ng serve --proxy-config proxy.conf.json"

Everything works on my dev environment.

But when I build for production :

ng build --prod

and deploy on firebase

firebase deploy

The web application is not able to communicate with the API

How can I do to fix it ?

like image 408
Franky Avatar asked Dec 06 '16 12:12

Franky


People also ask

What is the use of proxy config in Angular?

Use the proxying support in the webpack development server to divert certain URLs to a backend server, by passing a file to the --proxy-config build option. For example, to divert all calls for http://localhost:4200/api to a server running on http://localhost:3000/api , take the following steps.

What happens on NG build?

ng buildlink. Compiles an Angular application or library into an output directory named dist/ at the given output path.

What is NG Build command?

The ng build command is intentionally for building the apps and deploying the build artifacts. The command does not generate an output folder. The output folder is – dist/. The ng serve builds artifacts from memory instead for a faster development experience.


1 Answers

In case anybody else run into this problem, I found a workaround in case you still want to run locally your production angular application, use the http-server application:

npm install -g http-server

Build your production application:

ng build --prod

Go to your dist/[YOUR_APP_NAME_FOLDER] folder:

cd dist/[YOUR_APP_NAME_FOLDER] 

Run the http-server application and pass the Proxy option (--proxy or -P) with the backend proxy you would like to point to:

 http-server -c-1 -P http://localhost:3000 .
like image 70
vhbazan Avatar answered Sep 19 '22 10:09

vhbazan