9

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"
  }
}

4 Answers 4

3

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;
Sign up to request clarification or add additional context in comments.

2 Comments

This doesn't work for me with Angular - but then I can't see any way to configure http-proxy-middleware for an out-of-the-box Angular application?
angular uses the proxy support in the weback development server. Which is http-proxy-middleware. See documentation: angular.io/guide/build#proxying-to-a-backend-server
1

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.

Comments

1

It works if you set devServer.index to a falsy value.

https://webpack.js.org/configuration/dev-server/#devserver-proxy

Note that requests to root won't be proxied by default. To enable root proxying, the devServer.index option should be specified as a falsy value:

module.exports = {
    //...
    devServer: {
        index: '', // specify to enable root proxying
        host: '...',
        contentBase: '...',
        proxy: {
        context: () => true,
        target: 'http://localhost:1234'
        }
    }
};

2 Comments

How do you do this in an out-of-the-box Angular application? The devServer configuration doesn't appear to be exposed by default?
Never mind - I've figured it out. I can add devServer to my customWebpackConfig (using the Angular custom-webpack builder)
1

Update

The solution below worked for me at time of writing, but no longer seems to work with later versions of Webpack/Angular.

@Zygimantas has suggested a solution in the comments below, but unfortunately that doesn't work for me either.


I got this to work as follows:

In angular.json:

"architect": {
  "build": {
    "builder": "@angular-builders/custom-webpack:browser",
    "options": {
      "customWebpackConfig": {
        "path": "./my-custom-webpack.config.js"
      },

  ...

  "serve": {
    "builder": "@angular-builders/custom-webpack:dev-server",
    "options": {
      "proxyConfig": "webpack-proxy.conf.js",

  ...

In my-custom-webpack.config.js:

module.exports = {
  ...
  devServer: {
    index: '' // Allows us to proxy the root URL
  }
}

In webpack-proxy.conf.js

const PROXY_CONFIG = [{
  context: [
    '/'
  ],
  target: 'http://proxy-to-url',
  bypass: function(request) {
    // proxy the root URL, plus "/api/*" - otherwise serve using Angular
    return /^\/(api\/.*)?$/.test(request.url) ? null : '/index.html';
  },
  secure: false
}];

module.exports = PROXY_CONFIG;

(Needs @angular-builders/custom-webpack installed: npm i -D @angular-builders/custom-webpack)

4 Comments

my-custom-webpack.config.js should be: module.exports = { devServer: { devMiddleware: { index: "", }, }, };
@Zygimantas should it? Why? The above works fine for me.
This solution doesn't work if I don't change the webpack.config.js according to suggestion of @Zygimantas. I assume, they changed the configuration in webpack. Also see documentation: webpack.js.org/configuration/dev-server/#devserverproxy
Hmm - there has been a change in webpack and/or Angular, which means my solution above doesn't work any more. I've tried using @Zygimantas' solution, but that doesn't work for me either... :-(

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.