How to redirect MirajeJS to a real request if there is no such route in mocks?

268 Views Asked by At

With the help of mirajeJS, we can simulate routes, but is it possible, if such a route is not simulated in mirajeJS, to redirect it and use a real server using axios?

import {Server} from "miragejs";

new Server({
  routes() {
    this.namespace = "api/";
    this.get("test/router", () => {
      return [
        {name: "Angy", surname: "T."},
        {name: "Chris", surname: "B."},
        {name: "Juliana", surname: "Crain"}
      ];
    });
  }
});

That is, I want to achieve the following behavior, I wet the test / router request, but if the user makes any other request that is not locked in Miraje, then it goes to the real server, that is, the real request is used

1

There are 1 best solutions below

0
On

You could use a passthrough like this

new Server({
  routes() {
    this.namespace = "api/";
    this.get("test/router", () => {
      return [
        {name: "Angy", surname: "T."},
        {name: "Chris", surname: "B."},
        {name: "Juliana", surname: "Crain"}
      ];
    });
    // Allow unhandled requests on the current domain to pass through
    this.passthrough();
  }
});