How to disable HTTP-Mock server within environment config file

Hi,

I want to use HTTP-mocks while development but sometimes I have to test our backend API. Now I want to know how it’s possible to disable the mock server.

1 Like

I believe that you can do it by passing the proxy flag.

ember server  --proxy http://127.0.0.1:8080 

This would divert api requests to the specified address.

I haven’t needed to do this, so haven’t tried it and apologies if it doesn’t fix you problem :smile:

Thanks but that’s not working.

I tried several things but nothing works. I also tried to create an http-proxy but nothing happens.

I want to add a few additional information:

  1. I use ember-data
  2. I want to proxy every request to mock server

@dennismende - Wrap your the section in your server/index.js in an if and check for an environment variable.

Replace the following line

 mocks.forEach(function(route) { route(app); });

With something like this:

if (process.env.DISABLE_MOCK !== 'true') {
  mocks.forEach(function(route) { route(app); });
}

Then use that ENV variable to enable or disable the mocks as desired:

DISABLE_MOCK=true ember serve
3 Likes

Thank you for your reply. That helps me a lot but I have an additional problem.

If I user ember server --proxy … all ember-data request aren’t proxied. Do I forget anything?

Ok i found the problem. I set the host property of the application-adapter to my backend URL.

If you do that the http-mocks aren’t working.

IMO ember serve --proxy http://host:port/ should disable http-mocks and http-proxies by default.

1 Like

@rwjblue, @samwoodard: I had a similar requirement to use http-mocks by default, but make requests to a real server when provided one via --proxy. By slightly tweaking @rwjblue’s solution, I’ve come up with the following which seems to be working well:

function usingProxy() {
    return !!process.argv.filter(function (arg) {
        return arg.indexOf('--proxy') === 0;
    }).length;
}

/* ... */

if (usingProxy()) { return; }

mocks.forEach(function(route) { route(app); });
proxies.forEach(function(route) { route(app); });
8 Likes

This is great! I still think this should be the default behavior when creating using the mock server.

Thank you! This resolved a problem I was having as well!!