Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular Proxy in root level

I have an Angular application and a landing page written in jQuery.

I want to serve the landing page every time the request is on the root of http://mywebsite.com. Anything else, let's say http://mywebsite.com/otherpage, I want to serve the Angular application.

With nginx (production), this is fairly easy.

When developing Angular, we can easily proxy other requests, like /api, with --proxy-config proxy.config.json CLI option.

The problem I'm getting is with the root level.

What am I doing wrong? Can't I proxy the root level?

Angular docs tells us to go to Webpack dev-server docs, but still I couldn't figure out how to do it.

My proxy.config.json is like the following:

{
  "/api/*": {
    "target": "http://localhost:3000",
    "secure": false,
    "logLevel": "debug"
  },
  "/": {
    "index": "",
    "target": "http://localhost:3000",
    "secure": false,
    "logLevel": "debug"
  }
}
like image 929
Gustavo Lopes Avatar asked Dec 16 '18 19:12

Gustavo Lopes


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.

What does proxy Conf JSON do?

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.

What is environment folder in angular?

An Angular Application Environment is JSON configuration information that tells the build system which files to change when you use ng build and ng serve . Let's say you have a back end REST API deployed on a server that provides services to your Angular application.


2 Answers

I got a working proxy config which just intercepts the root and not a angular application served with baseHref. You need to use a .js config (https://angular.io/guide/build#proxy-multiple-entries) with an exclusion defined for http-proxy-middleware (https://github.com/chimurai/http-proxy-middleware#context-matching). The config below proxies every request expect when it starts with /otherpage. The angular application should use '/otherpage' as baseHref

const PROXY_CONFIG = [
  {
    context: [
      "/**",
      "!/otherpage**"
    ],
    "target": "http://localhost:3000",
    "secure": false,
  }
];

module.exports = PROXY_CONFIG;
like image 75
c0l3 Avatar answered Sep 29 '22 22:09

c0l3


It looks like it was a conflict with the root level after all.

Changing to other level, I could make it work.

The documentation is not explanatory on this.

  • https://webpack.js.org/configuration/dev-server/#devserver-proxy
  • https://angular.io/guide/build#proxying-to-a-backend-server
like image 32
Gustavo Lopes Avatar answered Sep 29 '22 23:09

Gustavo Lopes