Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular CLI - Set Up production proxy for now deploying

I am currently using now.sh for deploying my app. Recently, I decided to move the baseUrl for requests to a proxy.config.json file.

{
  "/api/*": {
    "target": "http://localhost:3000",
    "secure": false,
    "logLevel": "debug"
  }
}

For deploying with now.sh I change the scripts in package.json to:

"start": "serve dist/ --single",
"build": "ng build --prod",

so, first, I did a ng build.

The thing is, I'm needing a different target for proxy.config.json, because I deployed an API and I want to use it in the production app

I was thinking on having two files proxy.config.dev.json and proxy.config.prod.json, but I don't know if I can target a different proxy with the cli. The problem is that I'm using serve for production and not ng serve so proxy-config is not available.

What do you suggest? How can I get to work it?

like image 671
FacundoGFlores Avatar asked Aug 26 '17 17:08

FacundoGFlores


People also ask

What is reverse proxy in Angular?

A reverse proxy is a server that retrieves resources for clients from one or more upstream servers. It typically places itself behind a firewall in a private network and forwards clients request to these upstream servers.

How does proxy Conf JSON work?

conf. json" }, You can use the proxying support in the webpack dev 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.


1 Answers

I have realized that is a better solution to use "environments" which came by default with angular-cli.

So I setup a baseUrl in both environments {dev, prod}. And then the builds will not change. Then you have to modify your services to use this env variable.

like image 100
FacundoGFlores Avatar answered Sep 20 '22 14:09

FacundoGFlores