edbras
edbras

Reputation: 4404

mirage.js will not work with sub url paths (only with root path) in angular

Mirage does match defined routes when the backend calls are performed through the root url (localhost:4200/), but when we perform a backend call on a sub page, e.a.: localhost:4200/process, it will not work. When debugging the mirage code it will try to match by prefixing the url to be matched with "process". So in case BE call is "api/portal/affiliates/", it will try to match "process/api/portal/affiliates/" which fail as there doesn't exists a matching path. Any idea how to solve this.

We are using mirage.js in angular 9. The roue mirage.js config:

    routes() {
      this.passthrough();

      this.namespace = '/api/portal;
      this.get('affiliates', (schema, request) => {
        return schema.db.affiliates;
      })
}

Upvotes: 2

Views: 1927

Answers (1)

Kalpesh Shingala
Kalpesh Shingala

Reputation: 376

I am integrating mirage in Angular-9.

I was facing the same issue. Every time page gets refreshed on any URL with some routes it throws the error that mirage cannot find the route.

The issue was not with mirage. The issue was how I called the APIs.

Instead of doing

this.http.get('api/portal')

try to do as below

this.http.get('/api/portal')

Add / ( forward slash ) while calling an API. This solved my problem. Let me know if it still does not resolve your issue.

Upvotes: 0

Related Questions