5

To test client's ApiService class I need to replace my real backend URL by a mock and for these purposes I chose json-server. I set up a proxy config to forward all requests that starts with http://localhost:4200/v1/api to http://localhost:3000:

{
    "/api/v1": {
        "target": "http://localhost:3000",
        "secure": false
    }
}

And it works when I send requests like http://localhost:4200/api/v1/users BUT not for nested endpoints (e.g. http://localhost:4200/api/v1/auth/token). I found that json-server doesn't support requests to nested objects so I changed my data.json as below:

{
    "auth_token": {
        "access_token":"accesstoken1",
        "token_type":"Bearer"
    }
}

And set up routes.json for json-server:

{
    "/auth/token": "/auth_token"
}

But it doesn't work still through json-server uses routes.json:

[0]   Other routes
[0]   /auth/token -> /auth_token

What am I doing wrong?

Denis Sologub
  • 7,277
  • 11
  • 56
  • 123

1 Answers1

1

My solution didn't work due to my mistake and json-server's magic:

As I found json-server replaces /api/v1 to / automatically. But if you use routing settings above then /api/v1/auth/token won't be replaced to /api/v1/auth_token automatically and you will get 404 error.

So json-server behavior created an illusion for me that the proxy settings in Angular replaces http://localhost:4200/api/v1 to http://localhost:3000. Thus the right routing in my case looks as it's shown below:

{
    "/api/v1/auth/token": "/api/v1/auth_token"
} 
Denis Sologub
  • 7,277
  • 11
  • 56
  • 123
  • 1
    Quite annoying that nested routes are not supported. Then db.json will be a huge flat file without nesting, is that intentional? – trainoasis Jan 12 '21 at 08:29