3

I successfully implemented a proxy for my Angular CLI application. The proxy works well. The proxy configuration is in a proxy.conf.js file, and looks like this:

const PROXY_CONFIG = [
  {
    context: [
    "/api/assets/*",
    ],
    target: "http://localhost:4200/assets/",
    pathRewrite: {
    "/api": ""
    },
    secure: false,
    changeOrigin: true,
    logLevel: "debug"
  }
]

module.exports = PROXY_CONFIG;

Again, it is functional, and requests like /api/assets/data.json get redirected to http://localhost:4200/assets/data.json

What I try to achieve is to use a variable defined in my environment.ts instead of hardcoding the http://localhost:4200 URL.

However, I can't get it working. When trying to import a variable in the environment.tsfile with the standard import command

import {environment} from "./src/environments/environment";

I get the following error :

Unexpected token {
(function (exports, require, module, __filename, __dirname) { 
import {environment} from "./src/environments/environment";

Therefore my question : How to correctly import a variable from environment.ts into my proxy.conf.js file ?

Using :

  • Angular core v7.3.6
  • Angular CLI v7.3.6
  • Typescript v3.2.4
neophat
  • 187
  • 1
  • 10
  • Why do you need to do that? `--proxyConfig` is only available with `ng serve` which you use for your local machine. So, using `environment` file does not make sense for this config. – Bunyamin Coskuner May 22 '19 at 10:31
  • Indeed, for this specific example it wouldn't make much sense. But I'd like to retrieve automatically the port number (4200 by default) that would be stored as a variable in `environment.ts`, so a variable access is needed anyway. – neophat May 22 '19 at 14:21
  • You may need something from `environment` file during build processes but I still think you won't need to read anything from `environment` for the `proxy.conf.js`. You can simply write anything you want, it is for your local machine – Bunyamin Coskuner May 23 '19 at 05:12

0 Answers0