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.ts
file 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