'Deploy Angular App w/ Proxy for One Route

I have an Angular app w/ a proxy that's working well in local. There's one route that utilizes it with a Node API call.

Node.js

const express = require('express');
const api_service = require('./apiService');
const app = express();

app.get('/getData/:date', (req, res) => {
  const date = req.params.date;
  api_service.fetchData(...)
    .then(response => {
      res.json(response)
    })
    .catch(error => {
      res.send(error)
    })
})

app.listen(3000, () => {
  console.log("Server started in port 3000!");
});

Function in my Angular Service:

  fetchData(date: string) {
    const requestOptions: Object = {
      responseType: 'text',
    };
    this._http
      .get<string>('/api/getData/' + date)
      .pipe(
        map((data) => {
          this.result = data;
        })
      )
      .subscribe((data) => {
        this.mediaSource.next(this.result);
        if (Object.keys(this.result).length != 0) {
          this.history.push(this.result);
        }
      });
  }

proxy-prod.config.json

{
    "/api/*": {
        "target": "https://example.com",
        "secure": false,
        "changeOrigin": true,
        "logLevel": "debug",
        "pathRewrite": { "^/api": "" }
    }
}

proxy-local.conf.json

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

angular.json

 "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "ui-dev:build",
            "proxyConfig": "proxy-local.conf.json"
          },
          "configurations": {
            "production": {
              "browserTarget": "ui-dev:build:production",
              "proxyConfig": "proxy-prod.config.json"
            }
          }
        },

ng serve works perfectly. ng serve --prod and ng build --aot --prod --output-hashing none do not.

The error I'm getting is:

headers: d, status: 200, statusText: 'OK', url: 'myexample.com', ok: false

I've tried this in my request but they cause errors too:

const headers = new HttpHeaders().set('Content-Type', 'text/plain; charset=utf-8');
{ headers, responseType: 'text'}

Been searching, testing, and dissecting code for many hours and not sure how to proceed. Can you anyone assist?

Thanks



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source