Ember CLI: how to force proxy to override http-mock
In the Ember CLI, if the http-mock endpoint is defined, I cannot get the proxy setting to override it. For example:
At the beginning of the project, I run ember CLI like this:
$ ember s --proxy http://localhost:3000
Then my API requests are /api/leads
successfully proxied to http://localhost:3000/api/leads
. But as soon as I add the http-mock, the http-mock will always rule:
$ ember g http-mock leads
Even if I run the Ember CLI with a proxy ( ember s --proxy http://localhost:3000
) setting , the call /api/leads
will always be handled by http-mock and will not be proxied. The only way I can get the proxy to work again is by removing the http-mock links.
Is there a way to force the proxy setting to override the http-mock? I would like to switch between using http-mock and proxy while developing a "real" API, and I was hoping a simple installation --proxy http://localhost:3000
would allow me to do this.
source to share
We can use the Node environment variable to disable http-mock. In /server/index.js add this at the top:
if (process.env.DISABLE_MOCK === 'true')
return;
/server/index.js should now look something like this:
module.exports = function (app) {
var globSync = require('glob').sync;
var bodyParser = require('body-parser');
var mocks = globSync('./mocks/**/*.js', { cwd: __dirname }).map(require);
var proxies = globSync('./proxies/**/*.js', { cwd: __dirname }).map(require);
if (process.env.DISABLE_MOCK === 'true')
return;
// other code here
}
Now if we want to disable the HTTP mock and proxy for all ajax requests, you can start the Ember CLI like this.
$ DISABLE_MOCK=true ember s --proxy http://localhost:3000
This method was borrowed from @rwjblue from here: http://discuss.emberjs.com/t/how-to-disable-http-mock-server-within-environment-config-file/6660/5
source to share