How to define port dynamically in proxy.conf.json or proxy.conf.js

Viewed 2278

I have angular4 application, and for development purposes i start it with npm run start with start defined as "start": "ng serve --proxy=proxy.conf.json". Also I have

{
    "/api/**": {
      "target": "http://localhost:8080/api/"
    }
}

defined in proxy.conf.json. Is there a way, to define port or whole url dynamically. Like npm run start --port=8099?

PS

http://localhost:8080/api/ is URL of my backend API.

2 Answers

I also did not find way to interpolate .json file but you can use .js file

e.g.

proxy.conf.js

var process = require("process");
var BACKEND_PORT = process.env.BACKEND_PORT || 8080;
const PROXY_CONFIG = [
    {
        context: [
            "/api/**"
        ],
        target: "http://localhost:"+BACKEND_PORT+"/api/
    },
];
module.exports = PROXY_CONFIG;

add new command to package.json (notice .js not .json)

"startOn8090": "BACKEND_PORT=8090 && ng serve --proxy=proxy.conf.js"

or simply set env variable in your shell script and call npm run-script start

Related