Argus9
Argus9

Reputation: 1955

Ember.js - get value of `--proxy` parameter passed into `ember server`

I need my Ember app to make an AJAX request to the API backend it is proxying to. I have some kludgy code that inserts one of several hard-coded values but I'd much rather the AJAX request just grabs the value passed into --proxy when I run ember server.

Does anyone know if there's a way to retrieve this value from within Ember?

Upvotes: 0

Views: 437

Answers (2)

Charles
Charles

Reputation: 11778

You can get it in the file config/environment.js.

const proxy = (process.argv.indexOf('--proxy') != -1) ? process.argv[process.argv.indexOf('--proxy') + 1] : '';

Upvotes: 1

Chris de Almeida
Chris de Almeida

Reputation: 458

It appears it is not exposed, at least not by any public API. If you don't mind a bit of duplication at the command line, this would work:

$ proxy=http://myproxy ember server --proxy http://myproxy

The env params need to come before ember server or else ember throws them away.

// app/config/environment.js

const proxy = process.env.proxy;

const ENV = {
  APP: {
    // Here you can pass flags/options to your application instance
    // when it is created
    proxy,
  },
};
// app/routes/my-route.js (or wherever)

import ENV from 'myAppName/config/environment';

export default Route.extend({
  model() {
    return ENV.APP.proxy;
  },
});

You can of course access process.env.proxy from anywhere, but this is cleaner and keeps the property where it belongs.

Upvotes: 1

Related Questions